Practice
1. In a multi-threaded parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same spot?medium
Solution
Step 1: Identify race condition
Without synchronization, both threads can read spot availability as true before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch; Thread 2: isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability then park must be atomic to avoid double-booking [check]
Common Mistakes:
2. Consider a
tryConsume() method in TokenBucketRateLimiter that checks if tokens are available and then decrements the count without synchronization. Which exact thread interleaving causes double token consumption?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, both threads can read tokens > 0 before either decrements.Step 2: Trace exact interleaving causing double consumption
Thread 1 checks tokens > 0 -> true, context switch; Thread 2 checks tokens > 0 -> true; both proceed to decrement, consuming same token twice.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double token consumption [check]
Hint: Check tokens > 0 twice before decrement -> double consume [check]
Common Mistakes:
3. The vending machine currently applies a flat pricing strategy. To add a
WeekendFeeStrategy that charges extra on weekends, which minimal change respects SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingPricingStrategyand inject it without modifying existing classes. adds a new strategy class without modifying existing code, respecting OCP. Others modify existing classes or mix concerns.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends pricing without modifying existing code [check]
Hint: OCP: extend pricing via new class, don't modify existing [check]
Common Mistakes:
4. Adding a
WeekendFeeStrategy to calculate weekend parking fees requires which minimum change to the current design that uses a FeeStrategy interface?medium
Solution
Step 1: Understand current design with FeeStrategy interface
FeeStrategy interface allows different fee calculation implementations, supporting OCP if new strategies are added as new classes.Step 2: Analyze minimal change for weekend fees
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies modifies ParkingLot to add weekend logic, violating OCP but minimal change.Final Answer:
Option D -> Option DQuick Check:
Minimal change but violates OCP -> modify ParkingLot with weekend flag [check]
Hint: Minimal change โ OCP compliance; adding flag is minimal but not ideal [check]
Common Mistakes:
5. 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:
