Design patterns

OOP ยท 11 interview questions

Design patterns are named solutions to problems that recur in object-oriented design. They are usually grouped as creational (how objects are made), structural (how objects are composed) and behavioural (how objects interact).

Interviews rarely ask you to implement one from memory. They ask which pattern fits a situation, which is a question about the problem rather than the code โ€” so the useful thing to remember for each is the pressure it relieves.

It's also worth being able to criticise them. Singleton in particular is widely regarded as a liability, because it introduces global state and makes testing harder, and saying so demonstrates more than reciting its structure.

Design patterns interview questions

What are the three categories of design pattern?
Creational (object construction โ€” factory, builder, singleton), structural (composition โ€” adapter, decorator, facade) and behavioural (interaction โ€” observer, strategy, command).
What is the singleton pattern, and why is it criticised?
It ensures a class has exactly one instance with a global access point. It's criticised because it is global mutable state: it hides dependencies, makes tests order-dependent, and is awkward to substitute with a fake.
What problem does the factory pattern solve?
It moves the decision of which concrete class to construct out of the calling code, so callers depend on an interface and new implementations can be added without editing every construction site.
When would you reach for a builder?
When an object has many optional parameters, so constructor overloads multiply and long argument lists become unreadable. A builder names each value at the call site and can validate before constructing.
What is the observer pattern?
One object keeps a list of dependents and notifies them when its state changes, so subscribers react without the subject knowing anything about them. Event listeners and pub/sub are the everyday form.
What is the strategy pattern?
Encapsulating interchangeable algorithms behind one interface so the choice can be made at run time โ€” different sorting or pricing rules selected by injecting a different implementation rather than branching.
What is the decorator pattern?
Wrapping an object in another with the same interface to add behaviour without changing the original. Buffered and compressed stream wrappers are the canonical example, and they can be stacked.
What is the adapter pattern?
A wrapper that translates one interface into another so two components with incompatible APIs can work together, typically when one is third-party and cannot be changed.
What is a facade?
A single simplified interface over a complicated subsystem. It doesn't remove the complexity; it gives the common path one entry point so callers don't have to orchestrate several classes.
How do adapter and facade differ, given both are wrappers?
An adapter converts an existing interface into a different expected one โ€” the shape changes, the scope doesn't. A facade invents a simpler interface over many classes โ€” the scope narrows to the common case.
Why they ask: A precise follow-up that separates memorised lists from understanding.
What's the risk of applying patterns eagerly?
Indirection with no payoff. A factory with one implementation and a strategy with one algorithm add layers to read without buying flexibility. Patterns should answer a pressure the code is actually under.

You'll forget most of this by next week

That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ€” right before you'd lose them.

Start free for 7 days