Practice
Solution
Step 1: Identify the problem
The legacy system's interface is incompatible with the new platform.Step 2: Understand pattern intents
Adapter converts one interface to another, enabling integration without changing legacy code. Facade simplifies a complex subsystem but doesn't change interfaces. Proxy controls access, not interface compatibility. Decorator adds behavior dynamically, unrelated to interface mismatch.Final Answer:
Option B -> Option BQuick Check:
Adapter is the go-to pattern for interface incompatibility issues.
- Confusing Facade with Adapter because both provide a new interface
- Thinking Proxy changes interfaces rather than controlling access
- Assuming Decorator handles interface incompatibility
if-else statements to handle different payment methods like credit card, UPI, and net banking. The system needs to be extended frequently with new payment methods without modifying existing code. Which design approach best addresses this requirement?Solution
Step 1: Understand the problem of frequent extension
The system requires adding new payment methods without modifying existing code, which violates the open-closed principle if usingif-elsechains.Step 2: Identify the design pattern that encapsulates behaviors
The strategy pattern encapsulates each payment method in its own class implementing a common interface, allowing easy extension by adding new classes without changing existing code.Final Answer:
Option B -> Option BQuick Check:
Strategy pattern replaces conditionals with polymorphism [OK]
- Thinking recursion or greedy algorithms solve extensibility here
Solution
Step 1: Understand strict LSP enforcement
Strict LSP means subclasses cannot alter behavior or method contracts in incompatible ways.Step 2: Analyze trade-offs
This can restrict subclass implementations, making designs rigid and limiting specialization.Step 3: Evaluate options
It can lead to overly rigid designs that prevent useful specialization. correctly identifies rigidity as a trade-off. It always improves code flexibility and reduces bugs without downsides. is false; strict LSP can reduce flexibility. It allows subclasses to freely change method signatures for optimization. is incorrect; method signatures must respect variance rules. It eliminates the need for interface segregation. is false; interface segregation addresses different concerns.Final Answer:
Option A -> Option AQuick Check:
Strict LSP can constrain subclass design choices.
- Believing strict LSP has no downsides
- Confusing LSP with interface segregation
- Assuming method signatures can be freely changed
Solution
Step 1: Understand getter-only design
Getter without setter implies read-only access, often for immutability or controlled exposure.Step 2: Analyze interviewer intent
Interviewer probes if candidate recognizes design for immutability or encapsulation control.Step 3: Evaluate incorrect options
To confirm that the candidate knows getters always require setters is false; setters are not always required. To verify that the candidate believes private fields should never be exposed is extreme; exposing read-only data is common. To test if the candidate thinks public fields are better than getters is a misconception favoring public fields.Final Answer:
Option D -> Option DQuick Check:
Getter-only design supports immutability and controlled access.
- Assuming getters must have setters
- Thinking private fields must never be exposed
- Believing public fields are simpler and better
Solution
Step 1: Understand requirement
Need to optionally skip multiple steps dynamically while preserving fixed sequence and reuse.Step 2: Evaluate options
Adding multiple hook methods in base class allows subclasses to override selectively without breaking skeleton.Step 3: Reject other options
Overriding entire template method duplicates code and breaks pattern; flags break encapsulation; removing base class loses reuse.Final Answer:
Option B -> Option BQuick Check:
Multiple hooks preserve flexibility and structure [OK]
- Overriding template method to skip steps
- Using flags breaking encapsulation
