Practice
1. You are designing a
Vehicle abstraction for a parking system. Should Vehicle be an abstract class or an interface if you need to store state like licensePlate and provide default behavior for getSize()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
The design requires storing state (licensePlate) and providing default behavior (getSize()).Step 2: Choose correct OOP construct
Abstract classes can have instance variables and concrete methods with default behavior. Interfaces in Java 8+ can have default methods but cannot hold instance state. Therefore, abstract class fits the need.Final Answer:
Option C -> Option CQuick Check:
Abstract class supports state and default behavior [check]
Hint: State needs abstract class; interfaces lack instance variables [check]
Common Mistakes:
2. A class has a private constructor and a
clone() method that returns a copy of itself. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and clone()
The private constructor prevents direct instantiation, and the presence of aclone()method indicates object copying rather than new creation.Step 2: Match pattern intent to scenario
The Prototype pattern is used to clone objects when construction is expensive, which fits the scenario. Singleton restricts to one instance, Factory creates new instances, and Builder separates construction steps.Final Answer:
Option D -> Option DQuick Check:
Private constructor + clone() -> Prototype pattern [check]
Hint: Private constructor + clone() means Prototype pattern [check]
Common Mistakes:
3. The current Facebook design uses a
PrivacyStrategy interface for post visibility. To add a WeekendPrivacyStrategy that restricts posts only on weekends, what is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Identify applicable SOLID principle
Open/Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Apply OCP to privacy strategies
Adding a new class implementingPrivacyStrategyextends functionality without changing existing classes.Final Answer:
Option B -> Option BQuick Check:
New class for new behavior -> OCP compliance [check]
Hint: OCP: extend with new class, don't modify existing [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate parking fees differently on weekends using the current Template Method design. What is the minimum change that respects SOLID principles?medium
Solution
Step 1: Identify applicable SOLID principle
Open/Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Use strategy injection
Creating a newFeeStrategyclass for weekends and injecting it respects OCP and Dependency Inversion Principle (DIP).Final Answer:
Option C -> Option CQuick Check:
New strategy class added; no existing class modified [check]
Hint: OCP: add new class, don't modify existing [check]
Common Mistakes:
5. The system needs to support
ElectricCar playback devices that require ChargingSpot management. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes respects OCP and avoids class explosion or god-class logic.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
