Practice
1. In designing the
Player abstraction for the Tic-Tac-Toe game, should Player be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze state and behavior requirements
Players have common state (e.g., name, symbol, score) and some shared behavior (e.g., basic validation). Abstract classes allow state fields and shared method implementations.Step 2: Why abstract class is better than interface here
Interfaces define behavior contracts but cannot hold instance state. Although Java 8+ interfaces support default methods, they cannot hold mutable state. Thus, abstract class suits better for shared state and behavior.Final Answer:
Option C -> Option CQuick Check:
Shared state + behavior -> abstract class, not interface [check]
Hint: State needs abstract class; behavior-only fits interface [check]
Common Mistakes:
2. In the Food Ordering System, the
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact 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 available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [check]
Hint: Check availability -> context switch -> both assign same slot [check]
Common Mistakes:
3. In the Elevator System, the
assignElevatorToFloorRequest() method checks if an elevator is available before assigning it. Without synchronizing this method, what exact sequence of thread interleaving can cause two threads to assign the same elevator simultaneously?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads elevator as available, then context switches before assignment.Step 2: Thread 2 also reads elevator as available and proceeds to assign it, causing double-booking.
Both threads assign the same elevator because the availability check and assignment are not atomic.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act causes double assignment [check]
Hint: Check-then-act without sync allows double assignment race [check]
Common Mistakes:
4. The system must support
ElectricCar vehicles needing ChargingSpot parking. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code, respecting OCP.Step 2: Update compatibility externally
UpdatingSpotCompatibilitymap outside existing classes avoids modifying their code, enabling new vehicle-spot matching.Final Answer:
Option D -> Option DQuick Check:
Extend enums + subclass + external map update -> no existing class changes [check]
Hint: Extend enums + subclass + external map -> no existing class changes [check]
Common Mistakes:
5. To support ElectricCar orders requiring ChargingSpots in the existing Order Management System, what is the minimum change that respects extensibility without modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and classes externally, updating compatibility map without changing existing code; Options B, C, D modify existing classes violating OCP.Final Answer:
Option C -> Option CQuick Check:
Adding new enums and classes externally respects OCP [check]
Hint: Add new enums and classes externally, no existing class changes [check]
Common Mistakes:
