Design Patterns (GoF and modern) Module 1
Why patterns, and when not to use them interview questions
Interview questions on why patterns, and when not to use them: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What are the four essential parts of a design pattern?
Answered in the lesson What a design pattern is (and is not)
-
Fresher
What is the difference between aggregation and composition in a UML class diagram?
Answered in the lesson Reading UML class and sequence diagrams
-
Fresher
What does "favour composition over inheritance" mean in practice?
Answered in the lesson The design principles behind the patterns
-
Fresher
Why do some design patterns disappear in languages with first-class functions?
Answered in the lesson How language features reshape patterns
-
Fresher
What is YAGNI, and how does it apply to design patterns?
Answered in the lesson When patterns hurt: cost, YAGNI and overuse
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A teammate proposes "using more design patterns" to make a hard-to-change codebase easier to maintain. How do you respond, and what do you ask for instead?
- What it tests
- Treating patterns as answers to specific forces rather than as goals, and turning a vague request into named variations with costs.
- First step
- Ask which changes have been painful. List the last few changes that touched many files or broke something unexpected: a pattern is justified by a variation that keeps recurring, so the change history, not the catalogue, says where a pattern would pay.
- Follow-ups
-
- The team lead wants a factory for every class.
- How would you tell, months later, whether a pattern paid off?
- A new joiner says the code is already hard to follow.
From the lesson What a design pattern is (and is not)
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Is MVC a design pattern or an architecture style? Defend your answer, and say why the difference matters when a team discusses a design.
- What it tests
- Telling the levels of design apart, knowing how MVC combines smaller patterns, and using the level to scope a design discussion.
- First step
- Say that the name is used at two scales. Inside one user-interface component, MVC is a combination of smaller patterns: the views observe the model, views nest, and a controller decides how a view responds to input. As the layout of a whole web application, it is an architecture style.
- Follow-ups
-
- Where do MVP and MVVM fit?
- What does a web framework mean by a controller?
- Name another idea whose name is used at both levels.
From the lesson What a design pattern is (and is not)
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A design document draws every relationship between its classes as a plain line. What do you ask before writing code from it, and which answers change the code?
- What it tests
- Reading relationships as decisions about ownership, lifetime and direction, and turning a vague diagram into concrete questions with code consequences.
- First step
- For each line ask three questions: who creates the object at the other end, whether it outlives this one, and which side calls the other. Those answers choose between composition, aggregation and a plain association, and the direction of the arrow, and each choice changes a constructor or a field.
- Follow-ups
-
- Two classes point at each other.
- The diagram shows Manager inheriting from Employee.
- Where would you write down the answers?
From the lesson Reading UML class and sequence diagrams
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A class diagram shows composition, a filled diamond, from Customer to Order. Is that right? What would the code and the data have to do if it were?
- What it tests
- Applying the strict meaning of composition, spotting a modelling error, and knowing which business rules would make the diagram right.
- First step
- Composition means an order belongs to exactly one customer and is deleted with that customer. Ask whether orders must survive the customer's account being closed. Invoices and tax records usually must, so a plain association with a multiplicity is the honest model.
- Follow-ups
-
- The customer closes their account.
- Orders can be moved to another customer.
- What do the database tables look like in each model?
From the lesson Reading UML class and sequence diagrams
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
java.util.Stack extends Vector. What goes wrong with that design, how would you build a stack instead, and when is extending a library's base class the right choice?
- What it tests
- Spotting inheritance used only to reuse code, the invariants it leaves unprotected, and the difference between a class designed for extension and one that is not.
- First step
- List what a Stack inherits from Vector: methods that insert, remove or read at any index. Each one lets a caller break the last-in, first-out rule, and none can be removed, because a Stack must still work wherever a Vector is expected.
- Follow-ups
-
- What does the Java documentation recommend instead of Stack?
- When is extending AbstractList the right call?
- Python: is subclassing list to build a stack the same mistake?
From the lesson The design principles behind the patterns
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Java's List.copyOf returns a list whose add() throws UnsupportedOperationException. Does that break the Liskov substitution principle? Defend your answer, and say what you would do in an API of your own.
- What it tests
- Reading a contract precisely, telling a documented optional operation from a broken promise, and designing types that make capabilities visible.
- First step
- Read the contract, not only the method name. The Collection interface documents add as an optional operation that may throw UnsupportedOperationException, so a list that refuses it keeps the promise as written. Then ask what that contract costs the callers who rely on it.
- Follow-ups
-
- Your own cache has a read-only view that callers pass where a cache is expected.
- How do you find subclasses that refuse operations in a large codebase?
- The base class is in a library you cannot change.
From the lesson The design principles behind the patterns
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A Python service has a Java-style PricingStrategy hierarchy of eight classes. Four take constructor settings such as a rate or a cap, and the strategy is chosen by name from a config file at start-up. Plan the refactor: what replaces each class, and how do you migrate safely?
- What it tests
- Choosing between functions, closures, partial application and small classes for configured behaviour, keeping selection from configuration safe, and migrating step by step.
- First step
- Sort the classes by what they carry. The four without settings are functions. The four with settings need their values bound somewhere: a closure, functools.partial or a small frozen dataclass with __call__. Then decide how a name in the config file becomes one of them.
- Follow-ups
-
- A rule needs two methods, price() and describe().
- A typo in the config file names a rule that does not exist.
- Other packages must be able to add rules.
From the lesson How language features reshape patterns
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
A Java team keeps forty visitors over a twelve-class syntax tree, and plugins written by other teams add new node types. Would you replace the visitors with sealed interfaces and pattern-matching switch? Explain what you gain and what breaks.
- What it tests
- Seeing when a closed set of cases is an assumption you cannot make, comparing exhaustive matching with double dispatch, and designing a hierarchy that is closed in part.
- First step
- Start from the plugins. A sealed interface lists every permitted subtype, and they must live in the same module or package, so plugin node types cannot be permitted. That rules out sealing the whole tree; ask which part of it is really closed.
- Follow-ups
-
- Only the core node types are fixed; plugins add a few rare ones.
- Plugins also need to add new operations.
- How do you migrate forty visitors without a big-bang change?
From the lesson How language features reshape patterns
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A pull request adds a PaymentProviderFactory with one product and a PaymentProvider interface with one implementation, "for the future". How do you respond, and when would you accept it?
- What it tests
- Judging speculative generality with evidence, keeping the discussion constructive, and naming the conditions that justify a seam.
- First step
- Ask what the second provider is and when it arrives, and whether the tests need a fake provider today. If the answer to both is no, suggest calling the one provider directly behind a small function, and adding the interface when a second provider is real.
- Follow-ups
-
- The author says a second provider is planned for next quarter.
- The tests must not call the real provider.
- The team's style guide asks for an interface for every service.
From the lesson When patterns hurt: cost, YAGNI and overuse
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to senior
In a low-level design round, the interviewer asks why your design uses no pattern for sending notifications. How do you defend the choice, and what would make you add one?
- What it tests
- Defending simplicity with a named variation and a plan, instead of adding patterns to look thorough.
- First step
- State the variation you looked for and did not find: there is one channel today. Then say what would change your mind, such as a second channel or a choice per user, and how you would refactor: extract a channel interface and pass the channel in.
- Follow-ups
-
- The interviewer adds SMS and push notifications.
- Users choose a channel for each kind of message.
- A notification must be retried when a channel fails.
From the lesson When patterns hurt: cost, YAGNI and overuse
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
All Design Patterns (GoF and modern) interview questions · The Design Patterns (GoF and modern) track