Practice
1. In a Music Streaming Library design, a
Playlist class has a private constructor and a static synchronized getInstance() method that returns the single instance. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the pattern from private constructor and static synchronized getInstance()
This is the Singleton pattern, which restricts instantiation to one object and provides a global access point.Step 2: Why Singleton fits Playlist playback queue management
Since the playback queue must be globally consistent and unique per user session, Singleton ensures only one instance manages it, avoiding conflicting states.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() = Singleton [check]
Common Mistakes:
2. In the Elevator System design, the
ElevatorController class uses a private constructor and a static synchronized getInstance() method to provide a single global access point. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify Singleton usage
The private constructor and static synchronizedgetInstance()method restrict instantiation to one object, ensuring a single ElevatorController.Step 2: Why Singleton fits ElevatorController
ElevatorController coordinates multiple elevators and floor queues centrally, so multiple instances would cause inconsistent state and scheduling conflicts.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ElevatorController instance [check]
Hint: Singleton restricts to one instance for centralized control [check]
Common Mistakes:
3. In the ESPNcricinfo system, the
Match class has a private constructor and a static synchronized getInstance() method to ensure only one instance per match ID exists. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and getInstance()
The private constructor prevents direct instantiation, and the static synchronized getInstance() ensures controlled, thread-safe instance creation.Step 2: Why Singleton fits this use case
Singleton ensures only one Match instance per match ID exists, avoiding inconsistent state across threads.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
4. To support ElectricCar and ChargingSpot in the existing parking system using Chain of Responsibility, what is the minimum change that respects extensibility and avoids modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to avoid breaking OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility.COMPATIBILITY_MAP accordingly. modifies enums and compatibility map, changing existing code; Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds flags and updates handlers without changing existing class hierarchies; Create a new ElectricCarHandler in the chain that handles electric cars and charging spots, modifying the chain setup to include it. modifies chain setup, which is acceptable but may require modifying chain initialization; Add a new method in Vehicle interface for charging compatibility and implement it in ElectricCar, modifying existing handlers to call it. modifies Vehicle interface, breaking existing implementations.Step 3: Choose minimal invasive approach
Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds minimal changes with flags and updates handlers, avoiding class explosion or interface changes.Final Answer:
Option B -> Option BQuick Check:
Flags added, existing classes unchanged, handlers updated [check]
Hint: Add flags and update handlers, avoid modifying classes [check]
Common Mistakes:
5. The messaging system needs to support
ElectricCar users who require ChargingSpot delivery locations. 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 subclasses allows extension without changing existing class code, preserving OCP.Step 2: Understand how to update compatibility externally
Updating SpotCompatibility.COMPATIBILITY_MAP externally supports new types without modifying existing logic inside classes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclasses + external map update -> minimal change [check]
Hint: Extend enums and subclasses, update compatibility map externally [check]
Common Mistakes:
