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 Scenario: your checkout supports card and PayPal, and the business adds a new payment provider every quarter. How would you design it?
Use Strategy: a PaymentMethod interface with one implementation per provider, chosen through a lookup table or factory from the customer's choice. Wrap each vendor SDK in an Adapter so provider…
-
Most asked Scenario: when an order is placed, several systems (email, inventory, analytics, loyalty) must react, and more will be added. How do you design it?
Publish an OrderPlaced event and let each system subscribe: Observer in-process, or Pub/Sub through a broker if the reactions are slow, unreliable or in other services. Order placement no longer…
-
Scenario: you need undo/redo in a drawing or text editor. Which pattern would you use?
Command: each user action becomes an object with execute() and undo(), kept on a history stack, with a redo stack cleared on new actions. For complex operations that are hard to reverse, store a…
-
Scenario: a third-party SDK you must use has a completely different interface from the rest of your code. What do you do?
Write an Adapter that implements your own interface and translates calls, units, results and errors to the SDK. The rest of the code depends on your interface, so the SDK can be replaced or faked in…
-
Scenario: a class has a constructor with ten parameters, many optional. How would you improve it?
First check whether the class does too much (an SRP problem). If the object really needs that much configuration, use a Builder (named, chainable steps with validation in build()), or named…
-
Scenario: you need to add caching and logging to an existing service without modifying it. How?
Wrap it in Decorators that implement the same interface: LoggingService(CachingService(RealService())). Callers receive the wrapped object through dependency injection and don't know the difference.
-
Scenario: every method of an `Order` class starts with a switch on `status`. How would you refactor it?
Apply the State pattern: create a class per status implementing the allowed actions and transitions, and have Order delegate to its current state. Illegal transitions fail by default, and new…
-
Scenario: product categories contain products and subcategories to any depth, and you need totals per category. Which pattern fits?
Composite: a common CatalogNode interface implemented by Product (leaf) and Category (composite). A category's total() sums its children recursively, so callers treat one product and a whole…