Practice
1. In the brokerage system, should the
Order entity be designed as an abstract class or an interface? Consider that orders have common state fields like orderId and timestamp, but different types of orders have distinct execution logic.easy
Solution
Step 1: Analyze state vs behavior requirements
Orders share common state fields likeorderIdandtimestamp, which require storage and possibly shared methods.Step 2: Why abstract class fits better
Abstract classes can hold state and provide default method implementations, reducing code duplication. Interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Shared state + default behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
2. In the Blackjack game, if the
placeBet() method is not synchronized, which exact thread interleaving causes a player to double-bet?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads balance sufficient and is preempted before updating; Thread 2 also reads balance sufficient and proceeds.Step 2: Both threads place bets reducing balance twice, causing double-bet
This classic check-then-act gap leads to inconsistent state and double deduction.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double bet race condition [check]
Hint: Check balance -> context switch -> both bet -> double deduction [check]
Common Mistakes:
3. In the Tic-Tac-Toe game server, the
makeMove() method is not synchronized. Which exact sequence of events between Thread 1 and Thread 2 can cause two players to place their marks on the same cell (double-booking)?medium
Solution
Step 1: Identify the race condition in makeMove()
Without synchronization, the check-then-act sequence is vulnerable: Thread 1 checks if cell is empty and is preempted before marking.Step 2: Trace Thread 2's actions
Thread 2 also checks the same cell, sees it empty, and proceeds to mark it. Both threads mark the same cell, causing double-booking.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap without sync -> double-booking race [check]
Hint: Check empty -> context switch -> both mark same cell [check]
Common Mistakes:
4. In a Singleton class without synchronized on
getInstance(), what EXACT sequence of Thread 1 and Thread 2 calls can cause two instances to be created?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see instance == null before either creates it.Step 2: Trace thread interleaving
Thread 1 checks instance == null -> true, context switch before creating instance. Thread 2 also checks instance == null -> true, proceeds to create a second instance, causing double instantiation.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized lazy init -> check-then-act race causes two instances [check]
Hint: Check-then-act without sync -> double instance creation [check]
Common Mistakes:
5. 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:
