Practice
1. In designing an Elevator system using the State pattern, should the ElevatorState be an abstract class or an interface, and why?
easy
Solution
Step 1: Analyze shared code in states
Elevator states often share common fields (e.g., reference to context) and helper methods, which favors an abstract class.Step 2: Evaluate interface limitations
Interfaces cannot hold state or provide default implementations (prior to Java 8), so they are less suitable if shared code exists.Final Answer:
Option B -> Option BQuick Check:
Shared code and state -> abstract class [check]
Hint: Shared code and state -> abstract class; behavior only -> interface [check]
Common Mistakes:
2. In the ParkingLot class, 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, Thread 2 also reads it as available.Step 2: Both threads proceed to assign the same spot concurrently
Because no lock protects the check and assignment, both parkVehicle calls succeed, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without lock -> double-booking race [check]
Common Mistakes:
3. In the
OrderBook class, the placeOrder() method checks if the order slot is available and then books it. Without synchronization on placeOrder(), which exact sequence of thread interleaving causes double-booking of the same slot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as free but is preempted before booking; Thread 2 also reads free and proceeds to book.Step 2: Why this causes double-booking
Both threads believe the slot is free and book it concurrently, causing inconsistent state and double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without lock -> double-booking [check]
Hint: Check slot free -> context switch -> both book same slot [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 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:
5. In a parking lot system using the Strategy pattern, if
parkVehicle() is NOT synchronized, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Identify 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
Because the state update is not atomic, both threads believe the spot is free and park simultaneously.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> double-booking due to unsynchronized parkVehicle() [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
