Practice
1. In the Restaurant Management System, the
TableManager class has a method reserveTable() that checks if a table is available and then marks it reserved. Without synchronizing reserveTable(), which exact sequence of thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads table as available but before reserving, Thread 2 also reads it as available.Step 2: Both threads proceed to reserve the same table
Because the reservation step is not atomic, both threads mark the table reserved, causing double-booking.Final Answer:
Option D -> Option DQuick Check:
Check-then-act without lock -> double-booking [check]
Hint: Check-then-act gap -> double-booking race [check]
Common Mistakes:
2. The auction system currently calculates fees with a fixed strategy. To add a
WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?medium
Solution
Step 1: Identify SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding WeekendFeeStrategy implementing FeeStrategy interface without modifying existing classes.
Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliance [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
3. You want to add a
WeekendFeeStrategy to the parking lot pricing system. Which minimum change respects SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate options
Modify the existingFeeCalculatorclass to add weekend logic inside. violates OCP by modifying existing class. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. respects OCP by adding new strategy class. Add a boolean flagisWeekendtoVehicleand adjust pricing accordingly. incorrectly mixes state into Vehicle, violating SRP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand override methods. adds subclass but may cause class explosion and tight coupling.Final Answer:
Option A -> Option AQuick Check:
New strategy class + no existing class change -> OCP respected [check]
Hint: Add new strategy class, avoid modifying existing code [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate weekend parking fees without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [check]
Hint: New strategy class + injection -> Open/Closed Principle [check]
Common Mistakes:
5. In a parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 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 but before marking it occupied, context switches to Thread 2 which also reads it as available.Step 2: Both threads proceed to park, causing double-booking
Because the state update is not atomic, both threads park vehicles in the same spot simultaneously.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act gap causes double-booking without synchronization [check]
Common Mistakes:
