Practice
1. In a vehicle rental system, should
Vehicle be designed as an abstract class or an interface if it needs to hold common state like licensePlate and rentalStatus? Why?easy
Solution
Step 1: Analyze state requirements
Vehicle needs to hold common mutable state likelicensePlateandrentalStatus, which interfaces cannot hold as fields.Step 2: Choose correct OOP construct
Abstract classes can have instance variables and default method implementations, making them suitable for shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option D -> Option DQuick Check:
Shared mutable state -> abstract class, not interface [check]
Hint: Shared state needs abstract class, interfaces only define behavior [check]
Common Mistakes:
2. 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:
3. 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:
4. To add a
WeekendFeeStrategy for weekend parking fees without modifying existing classes, which SOLID principle is being applied and what is the minimal required change?medium
Solution
Step 1: Identify SOLID principle
Open-Closed Principle (OCP) requires extending behavior without modifying existing classes.Step 2: Apply minimal change
Create newWeekendFeeStrategyimplementingFeeStrategyand inject it; no existing class changes needed.Final Answer:
Option B -> Option BQuick Check:
OCP: new fee class added, existing classes unchanged [check]
Hint: OCP: extend with new class, no existing code changes [check]
Common Mistakes:
5. The notification system must support
ElectricCar vehicles that require ChargingSpot notifications. What is the minimum design change to support this 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: Apply minimal changes
Adding new enum values and creating new subclasses allows extension without modifying existing class code; updating compatibility map centralizes new logic.Step 3: Eliminate other options
Options B, C, and D modify existing classes or factory logic, violating OCP and risking class explosion or god-class anti-pattern.Final Answer:
Option C -> Option CQuick Check:
New enums and subclasses extend design without modifying existing classes [check]
Hint: Add enums + subclasses, avoid modifying existing classes [check]
Common Mistakes:
