Practice
1. In a multi-threaded Chess game server, without synchronizing the
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads read the turn state before either updates it, causing both to believe it's their turn.Step 2: Trace thread interleaving
Thread 1 checks turn -> true, context switch before move; Thread 2 checks turn -> true, both move simultaneously, violating turn order.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap allows double move without synchronization [check]
Hint: Check turn then act without lock -> race condition [check]
Common Mistakes:
2. In the playback queue, the method
addTrack() is not synchronized. Two threads concurrently check if the queue has space and then add tracks. Which exact sequence causes a race condition leading to queue overflow?medium
Solution
Step 1: Identify the check-then-act pattern in addTrack()
Both threads check queue size before adding, but without synchronization, the state can change between check and add.Step 2: Trace the race condition sequence
Thread 1 checks size -> space available, context switch before adding. Thread 2 checks size -> space available, both add tracks, causing overflow.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double add -> overflow [check]
Hint: Check-then-act without sync -> race condition [check]
Common Mistakes:
3. In the Amazon Locker Service, if the
assignLocker() method is not synchronized, which exact sequence of thread interleaving causes two packages to be assigned the same locker?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads locker as available, then context switches before marking it occupied.Step 2: Thread 2 also reads locker as available and proceeds to assign it, causing double-booking.
Both threads assign the same locker before either updates the status.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap -> double assignment [check]
Hint: Check availability -> context switch -> both assign same locker [check]
Common Mistakes:
4. 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 but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Both threads believe the spot is free and park simultaneously, violating mutual exclusion.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
5. ElectricCar needs ChargingSpots in the Stack Overflow vehicle analogy. 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 subclass ElectricCar extends Vehicle allows extension without changing existing class code.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP supports new types without modifying core logic, preserving OCP.Final Answer:
Option D -> Option DQuick Check:
Enum extension + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
