Practice
1. To add a
WeekendFeeStrategy for weekend pricing in the current booking system design, which minimal change respects the Open-Closed Principle (OCP)?medium
Solution
Step 1: Identify OCP requirement
OCP requires adding new functionality without modifying existing tested code.Step 2: Evaluate options
Add a boolean flagisWeekendinBookingand handle fees there. adds weekend flag in Booking, which violates OCP by modifying Booking class. Create a new class implementingFeeStrategyinterface for weekend fees and inject it without changing existing classes. creates a new FeeStrategy class, respecting OCP by extension. Modify the existingFeeCalculatorclass to add weekend logic with if-else conditions. and D modify existing classes, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New FeeStrategy class -> extend without modifying existing code [check]
Hint: New strategy class -> extend, don't modify [check]
Common Mistakes:
2. In the Airline Reservation System, if the
reserveSeat() method is not synchronized, which exact sequence of thread interleaving causes double-booking of the same seat?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads seat as available but is preempted before booking; Thread 2 also reads available and proceeds.Step 2: Both threads book the same seat concurrently
Because no lock or atomic operation protects the check-and-book sequence, double-booking occurs.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check seat available -> context switch -> both book [check]
Common Mistakes:
3. The Blackjack game needs to support
ElectricCar players who require ChargingSpot betting spots. What is the minimum design change to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses externally allows extension without changing existing code, respecting OCP.Step 2: Why other options violate OCP or cause class explosion
Options B and D modify existing classes, violating OCP; Create a newElectricCarVehicleandChargingSpotclasses inheriting from existing ones and modify base classes to support them creates new classes but requires base class changes.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external compatibility map -> minimal change [check]
Hint: Extend enums + subclass + external map -> no existing class change [check]
Common Mistakes:
4. The system needs to support
ElectricCar and ChargingSpot without modifying existing classes. What is the minimum change to the current design to support this?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code.Step 2: Update SpotCompatibility map to include new types
Updating compatibility map is a configuration change, not a class modification, preserving OCP.Final Answer:
Option C -> Option CQuick Check:
Enum extension + new subclasses + compatibility map update -> no existing class change [check]
Hint: Extend enums + subclasses + config map -> no class edits [check]
Common Mistakes:
5. ElectricCar requires ChargingSpots in the parking system. What is the minimum change to support this without modifying existing classes, ensuring extensibility and avoiding class explosion?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclass ElectricCar extends Vehicle allows new types without changing existing code.Step 2: Update SpotCompatibility.COMPATIBILITY_MAP to include new compatibility rules for charging spots.
Final Answer:
Option D -> Option DQuick Check:
New enums and subclass added, existing classes unchanged -> extensible design [check]
Hint: Add enums and subclass; update compatibility map, no existing class changes [check]
Common Mistakes:
