Practice
1. In the in-memory file system design, a
FileSystem class has a private constructor and a static synchronized getInstance() method returning the single instance. Which pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Reason why Singleton fits this use case
Singleton is appropriate because the file system state must be globally consistent and unique; multiple instances would cause conflicting states.Final Answer:
Option B -> Option BQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In a parking lot system, if
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied. Thread 2 also reads available and proceeds to park.Step 2: Confirm both threads park concurrently causing double-booking
Both threads believe the spot is free and park, leading to inconsistent state and double-booking.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race condition [check]
Common Mistakes:
3. ElectricCar pieces require special
ChargingSpot compatibility in the Jigsaw Puzzle System. What is the minimum design change to support ElectricCar and ChargingSpot without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
New piece and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate minimal change approach
Adding new enum values and subclass for ElectricCarPiece plus updating compatibility map externally allows extension without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass extend system without changing existing classes [check]
Hint: Add enums + subclass + external map update = minimal invasive extension [check]
Common Mistakes:
4. The Workflow Engine must support
ElectricCar vehicles requiring ChargingSpot compatibility. What is the minimum change to the current design to add this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must not modify existing classes to respect OCP and avoid class explosion.Step 2: Use enums and subclassing
Adding new enum values and a new ElectricCar subclass extends functionality cleanly; updating compatibility map centralizes spot-vehicle matching without changing existing code.Step 3: Avoid modifying base classes or Executor
Modifying Vehicle or Executor violates OCP and risks introducing bugs.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + compatibility map update -> minimal, no existing class change [check]
Hint: Add enums + subclass + map update, no existing class change [check]
Common Mistakes:
5. ElectricCar needs to be added to the product catalog with ChargingSpots. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new classes for ElectricCar and ChargingSpot allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility.COMPATIBILITY_MAP controls which vehicles fit which spots; updating it ensures ElectricCar and ChargingSpot compatibility without modifying base classes.Final Answer:
Option B -> Option BQuick Check:
New enums + classes + compatibility map update -> minimal existing class changes [check]
Hint: Add enums + classes + map, no base class changes [check]
Common Mistakes:
