Design Patterns Interview Questions
Q: What are the three categories of design patterns?
Creational — object creation (Singleton, Factory, Abstract Factory, Builder, Prototype)
Structural — object composition (Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy)
Behavioral — communication between objects (Observer, Strategy, Command, Iterator, State, Chain of Responsibility)
Q: What is the difference between Observer and Pub/Sub?
Observer: subjects know their observers directly (direct coupling). Used in the same process. Pub/Sub: publishers and subscribers are completely decoupled through a message broker (they don't know about each other). Pub/Sub scales across processes and services (Redis pub/sub, message queues, EventEmitter). Observer is synchronous; pub/sub is often asynchronous.
Q: When would you use a Factory vs Builder pattern?
Use Factory when construction is simple and you just want to decouple the caller from knowing which concrete class to instantiate. Use Builder when construction is complex with many optional parameters, or when the same construction process should produce different representations. Builder also allows step-by-step construction and makes invalid states impossible.
Q: What is Dependency Injection and why is it useful?
DI is a technique where a class receives its dependencies from outside rather than creating them internally. Benefits: testability (inject mocks), loose coupling (depend on interfaces), flexibility (swap implementations). It's a form of Inversion of Control — the class doesn't control its own dependency creation. Frameworks like NestJS, Spring, and Angular have built-in DI containers.
Q: What is the SOLID principle?
S — Single Responsibility: a class should have one reason to change
O — Open/Closed: open for extension, closed for modification (add via new classes, not changing existing)
L — Liskov Substitution: subtypes must be substitutable for their base types
I — Interface Segregation: clients shouldn't depend on methods they don't use (many small interfaces > one large)
D — Dependency Inversion: depend on abstractions, not concretions
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free