Practice
1. In designing the Vehicle hierarchy for the booking system's parking module, should
Vehicle be an abstract class or an interface, and why?easy
Solution
Step 1: Identify if common state exists in Vehicle
Vehicle has shared fields like license plate, size, or type, which require storage and partial implementation.Step 2: Abstract class fits because it allows shared state and behavior, while interfaces cannot hold instance state.
Using an abstract class enforces common code reuse and prevents direct instantiation.Final Answer:
Option B -> Option BQuick Check:
Common state + shared behavior -> abstract class [check]
Hint: State needs abstract class; behavior-only needs interface [check]
Common Mistakes:
2. In the Hotel Management System, if the
parkVehicle() method is not synchronized, which exact sequence of events between Thread 1 and Thread 2 causes double-booking of the same parking spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied.Step 2: Thread 2 also reads spot as available and proceeds to park, causing double-booking.
This is the classic check-then-act gap causing race condition.Final Answer:
Option A -> Option AQuick Check:
Check availability -> true by both threads before marking occupied -> double-booking [check]
Hint: Check-then-act gap -> double-booking without synchronization [check]
Common Mistakes:
3. In the
FollowService, the method followUser() checks if the follower already follows the followee before adding. Without synchronization, which exact thread interleaving causes a double-follow entry?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads callisFollowing()and get false before either adds the follower, due to lack of synchronization.Step 2: Resulting double addition
Both threads proceed to add the follower, causing duplicate entries.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap -> both see false -> both add follower [check]
Hint: Check-then-act without sync -> double add race [check]
Common Mistakes:
4. Adding a
WeekendFeeStrategy to calculate weekend parking fees requires which minimum change to the current design that uses a FeeStrategy interface?medium
Solution
Step 1: Understand current design with FeeStrategy interface
FeeStrategy interface allows different fee calculation implementations, supporting OCP if new strategies are added as new classes.Step 2: Analyze minimal change for weekend fees
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies modifies ParkingLot to add weekend logic, violating OCP but minimal change.Final Answer:
Option D -> Option DQuick Check:
Minimal change but violates OCP -> modify ParkingLot with weekend flag [check]
Hint: Minimal change ≠ OCP compliance; adding flag is minimal but not ideal [check]
Common Mistakes:
5. ElectricCar requires ChargingSpots in the parking lot system. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
To comply with OCP and avoid class explosion, new types should be added via enums and new subclasses without changing existing code.Step 2: Explain why Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. fits minimal change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and creates a new ElectricCar subclass, updating compatibility externally, preserving existing classes unchanged. Options B, C, and D modify existing classes or create subclasses that duplicate logic, violating OCP or SRP.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Hint: Add enums + subclass + external map update -> no existing class changes [check]
Common Mistakes:
