Design Patterns & SOLID Interview Questions
Frequently asked design pattern and SOLID interview questions with concise answers and code-level explanations.
Design patterns & SOLID interview questions
Interviewers ask about patterns to see whether you can design maintainable code — so always connect a pattern to the problem it solves and a real example.
1. What is a design pattern?
A reusable, proven solution to a recurring design problem, described as a template rather than finished code. Patterns also provide a shared vocabulary between developers.
2. What are the three categories of GoF patterns?
Creational (object creation: Factory, Builder, Singleton), structural (object composition: Adapter, Decorator, Facade, Proxy, Composite) and behavioral (communication: Strategy, Observer, Command, State, Template Method).
3. Explain the SOLID principles.
Single responsibility (one reason to change), open/closed (extend without modifying), Liskov substitution (subtypes must be substitutable), interface segregation (small, client-specific interfaces) and dependency inversion (depend on abstractions; inject implementations).
4. Give an example of violating the Liskov Substitution Principle.
Square extending Rectangle: setting width then height on a square changes both sides, so code expecting a 5×4 area of 20 gets 16. The subclass breaks the parent's behavioural contract.
5. What is dependency injection and why use it?
Passing an object's dependencies in from outside (usually via the constructor) instead of creating them inside. It decouples code from concrete implementations and makes testing easy by injecting fakes. Frameworks like Laravel and Spring provide DI containers.
6. Factory vs Abstract Factory?
A factory creates one product, choosing the concrete class at runtime. An abstract factory creates families of related products that must be used together (e.g. AWS storage + AWS queue).
7. When would you use the Builder pattern?
When an object has many optional parameters or must be assembled step by step — e.g. SQL query builders, HTTP request builders. It avoids long constructors and supports validation before building.
8. Why is Singleton considered an anti-pattern by many?
It introduces global mutable state, hides dependencies, complicates testing and needs care in multi-threaded code. Prefer a single instance created at startup and injected where needed.
9. How do you make a Singleton thread-safe?
Create the instance eagerly at startup, or guard lazy creation with a lock (double-checked locking). In Python, a module-level instance is created once on first import.
10. Adapter vs Facade?
An adapter converts an existing interface into one the client expects (usually wrapping one class). A facade provides a new, simpler interface over a whole subsystem of several classes.
11. Decorator vs inheritance?
Decorators add behaviour at runtime by wrapping objects and can be combined freely (logging + caching + retry). Inheritance fixes behaviour at compile time and leads to class explosions for every combination.
12. Decorator vs Proxy?
Both wrap an object with the same interface. A decorator adds responsibilities; a proxy controls access — lazy loading, permissions, caching or remote calls.
13. Explain the Strategy pattern with an example.
Encapsulate interchangeable algorithms behind a common interface and choose one at runtime — e.g. shipping cost calculators (flat, per-km, promo) or payment methods. It removes if/else chains and follows open/closed.
14. Explain the Observer pattern. Where is it used?
A subject notifies registered observers when its state changes, without knowing what they do. Used in event listeners, Laravel events, UI frameworks, webhooks and pub/sub messaging.
15. How does the Command pattern relate to job queues?
A job is a command object containing everything needed to perform an action. It can be serialised, queued, retried, logged and executed later by a worker. Commands also enable undo/redo.
16. State pattern vs Strategy pattern?
Both delegate behaviour to interchangeable objects. In Strategy, the client chooses the algorithm. In State, the object changes its own state objects as it transitions, and each state controls which transitions are allowed.
17. Which patterns does HTTP middleware use?
Chain of Responsibility (each middleware handles or passes the request on) and Decorator (each layer wraps the next handler, adding behaviour before/after).
18. What is composition over inheritance?
Building behaviour by combining objects (has-a) rather than extending classes (is-a). It's more flexible, avoids fragile base classes and supports runtime changes.
19. What is an anti-pattern? Name some.
A common but counterproductive solution. Examples: god object, spaghetti code, golden hammer, premature optimisation, singleton abuse and copy-paste programming.
20. How do you avoid over-engineering with patterns?
Start with simple code, apply patterns when real duplication or change pressure appears, prefer language features (functions, modules) over boilerplate, and keep abstractions only when they pay for themselves.
Practice tip
Pick a small feature — like checkout with multiple payment methods and notifications — and implement it twice: once with if/else, once with Strategy and Observer. Explaining the trade-offs between the two versions is exactly what interviewers want to hear.