Interview Q&A · questions with model answers
Design Patterns Interview Questions
Part of the study plan for: Backend Engineer · Tech Lead
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked What is the difference between dependency inversion, dependency injection and inversion of control?
Dependency inversion is a design principle (high-level code depends on abstractions it owns). Dependency injection is a technique for supplying dependencies from outside (usually through the…
-
Most asked What is the difference between the Strategy and State patterns?
Both delegate to an interchangeable object, but in Strategy the client chooses the algorithm and strategies don't know each other, while in State the state objects themselves trigger transitions to…
-
Most asked Compare Adapter, Decorator, Proxy and Facade.
All four wrap something. Adapter gives an existing object a different interface so it fits; Decorator keeps the same interface and adds behaviour (stackable); Proxy keeps the same interface and…
-
Most asked What is the difference between Observer and Pub/Sub?
In Observer, the subject holds direct references to its observers and usually notifies them synchronously in the same process. In Pub/Sub, publishers and subscribers only know a broker and topic…
-
Most asked When should you NOT use a design pattern?
When the problem it solves isn't present yet: one implementation, no variation, or simple code that's easy to change later. Apply YAGNI and the rule of three, prefer language features (functions,…
-
Most asked Compare Factory Method, Abstract Factory and Builder.
Factory Method creates one product, with the concrete class chosen by a subclass; Abstract Factory creates a matching family of related products through one factory object; Builder creates one…
-
When would you use the Abstract Factory pattern?
When you must create families of related objects that have to match, such as light-theme or dark-theme widgets, or one database vendor's connection, statement and result types, and you want to…
-
When would you use a Decorator instead of subclassing?
When features need to be combined in different ways, chosen at runtime, or added to classes you can't change (final or third-party). Subclassing fixes one combination at compile time and needs a…
-
Template Method or Strategy: how do you choose?
Template Method varies steps through inheritance (subclasses override steps of a fixed skeleton); Strategy varies an algorithm through composition (an object or function is passed in). Prefer…
-
Which design patterns do you see in React?
Composite (components containing components), Observer (state subscriptions, useSyncExternalStore, context consumers), Strategy (passing functions and render props), Decorator (higher-order…
-
Which design patterns do Laravel and Spring use internally?
Laravel: service container (DI/IoC), facades (static proxies), middleware pipeline (Chain of Responsibility + Decorator), events and observers, query builder (Builder), Eloquent (Active Record),…
-
What is the difference between Active Record and Data Mapper?
In Active Record, a domain object also knows how to save and load itself (user.save()), as in Eloquent, Rails and Django's ORM. In Data Mapper, separate mapper/repository objects move data between…
-
Which design patterns does the Java standard library use?
Iterator (Collection.iterator()), Decorator (I/O streams like BufferedReader), Adapter (Arrays.asList(), InputStreamReader), Factory Method (Calendar.getInstance(), List.of()), Builder…
-
Which design patterns do Python features replace or simplify?
First-class functions make Strategy, Command and Observer often just callables; generators implement Iterator; modules act as singletons; decorators (@) wrap functions like the Decorator pattern;…
-
Which design patterns do you use in PHP and Laravel day to day?
Dependency injection through the service container, Strategy via interfaces bound to implementations (bind(PaymentGateway::class, ...)), Observer through events/listeners and model observers,…
-
Which design patterns are common in TypeScript and Node.js code?
Observer (EventEmitter, RxJS), Middleware/Chain of Responsibility (Express, Koa, NestJS interceptors), Dependency Injection (NestJS, InversifyJS), Strategy (functions and Passport.js strategies),…
-
Command or Memento for undo: which would you use?
Command undoes by reversing each action (it stores just enough to undo), which is memory-efficient for frequent small edits. Memento undoes by restoring snapshots of state, which is simpler and…
-
What is the difference between Bridge and Adapter?
Adapter is applied after the fact to make an existing class with an incompatible interface fit; Bridge is designed up front to separate an abstraction from its implementation so both can vary…
-
How do Composite and Decorator differ, given both use recursive composition?
Composite builds trees where a container holds many children and combines their results (a folder's size); Decorator wraps exactly one object to add behaviour while keeping its interface (logging…
-
When would you use a simple factory, and when a dependency injection container?
A simple factory decides which class to create from input at runtime ("csv" → CsvFormatter). A DI container wires an application's long-lived services and their dependencies at startup. They solve…