Practice
1. In designing the
Player abstraction for the Snake and Ladder game, should Player be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze state and behavior requirements
ThePlayerhas common state such as current position on the board and methods like move().Step 2: Choose abstraction type
An abstract class allows sharing state and default method implementations, which interfaces (pre-Java 8) cannot provide.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior [check]
Hint: State + default behavior -> abstract class, not interface [check]
Common Mistakes:
2. In a multi-threaded parking lot system, if
parkVehicle() is NOT synchronized, which exact sequence causes double-booking of the same spot?medium
Solution
Step 1: Identify the race condition
Without synchronization, both threads can read spot as available before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch before marking occupied; Thread 2: isAvailable() -> true, both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
3. To support
ElectricCar needing ChargingSpot in the existing rate limiter design, what is the minimum change that respects extensibility and avoids modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should avoid modifying existing classes to respect OCP.Step 2: Evaluate options for minimal change without modifying existing classes
Modify VehicleType enum and SpotType enum, then add charging logic inside existing Vehicle and Spot classes modifies enums and existing classes, violating OCP. Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally adds new enum values and classes, updating compatibility externally, preserving existing classes.Final Answer:
Option B -> Option BQuick Check:
Modifying enums and existing classes violates extensibility [check]
Hint: Add new enums and classes externally, avoid modifying existing classes [check]
Common Mistakes:
4. The system needs to support
ElectricCar and ChargingSpot without modifying existing classes. What is the minimum change to the current design to support this?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code.Step 2: Update SpotCompatibility map to include new types
Updating compatibility map is a configuration change, not a class modification, preserving OCP.Final Answer:
Option C -> Option CQuick Check:
Enum extension + new subclasses + compatibility map update -> no existing class change [check]
Hint: Extend enums + subclasses + config map -> no class edits [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the existing parking system using the Iterator pattern. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require 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; existing classes remain unchanged. adds new enums and classes without modifying existing ones, preserving OCP. Modify Vehicle and Spot classes to include electric-specific fields and logic, then update iterators accordingly. modifies existing classes, violating OCP. Create a new ElectricVehicle interface extending Vehicle and a ChargingSpot class, then modify existing iterators to handle them. adds interfaces but requires modifying iterators, violating OCP. Add a flag isElectric to Vehicle class and modify SpotCompatibility to check this flag during iteration. modifies Vehicle class, violating OCP.Final Answer:
Option A -> Option AQuick Check:
Adding new enums and subclasses without changing existing classes respects OCP [check]
Hint: Add new types via enums and subclasses, no existing class changes [check]
Common Mistakes:
