Practice
1. In designing the Chess game, should the
Piece abstraction be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze shared state among pieces
All pieces share common state such as position on the board and color, which is best encapsulated in a base class.Step 2: Evaluate abstract class vs interface
Abstract classes allow shared state and common method implementations, while interfaces primarily declare behavior without state.Final Answer:
Option D -> Option DQuick Check:
Shared state -> abstract class preferred over interface [check]
Hint: Shared state? Use abstract class, not interface [check]
Common Mistakes:
2. In the Airline Reservation System, the
SeatManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single globally accessible instance.Step 2: Why Singleton fits this use case
SeatManager must coordinate seat allocation consistently across the system, so a single instance avoids conflicting seat states.Final Answer:
Option C -> Option CQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. 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:
4. In a multiplayer card game, the
Hand class has a method addCard(Card card) that is not synchronized. Two threads concurrently add cards to the same hand. Which exact sequence causes a race condition leading to lost cards?medium
Solution
Step 1: Identify the check-then-act gap
Without synchronization, both threads read the current size of the card list before either adds their card.Step 2: Trace interleaving causing overwrite
Thread 1 reads size N, context switches; Thread 2 reads size N; both add at index N, causing one card to overwrite the other.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized addCard -> check-then-act race -> lost card [check]
Hint: Check size -> context switch -> duplicate index add -> lost card [check]
Common Mistakes:
5. ElectricCar needs to park in ChargingSpots. What is the minimum change to the current design with
VehicleType and SpotType enums, and SpotCompatibility map, to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new enum values and a new class without modifying existing classes to respect OCP.Step 2: Apply minimal change with enums and composition
Adding new enum constants and extendingVehiclewithElectricCarallows reuse of existing logic. UpdatingSpotCompatibility.COMPATIBILITY_MAPwith new mappings supports compatibility without changing existing code.Step 3: Eliminate options violating OCP
Options B, C, and D modify existing classes or require widespread changes, violating OCP and risking regressions.Final Answer:
Option B -> Option BQuick Check:
New enums + subclass + compatibility map update -> minimal, OCP-compliant [check]
Hint: Add enums + subclass + map update, no existing class changes [check]
Common Mistakes:
