Practice
1. You want to add a
WeekendFeeStrategy to calculate parking fees differently on weekends using the current adapter-based design. What is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new class implementing FeeStrategy. Add a boolean flag to Vehicle to indicate weekend and modify ParkingLot to check it. modifies Vehicle and ParkingLot, violating OCP. Options A and D modify existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
Hint: OCP: extend with new class, don't modify existing ones [check]
Common Mistakes:
2. The Wallet System currently calculates fees with a
FeeStrategy interface. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP to fee calculation
Adding WeekendFeeStrategy as a new class implementing FeeStrategy extends behavior without modifying existing classes, preserving OCP.Final Answer:
Option A -> Option AQuick Check:
New FeeStrategy implementation -> no existing class changes -> OCP [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
3. In a parking system, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Trace parkVehicle() check-then-act sequence
Thread 1 reads spot as available (true), then context switch occurs before marking occupied.Step 2: Thread 2 also reads spot as available (true) and proceeds to mark it occupied, causing double-booking.
Final Answer:
Option B -> Option BQuick Check:
Check-then-act race without synchronization -> double-booking [check]
Hint: Check-then-act gap causes double-booking without synchronized [check]
Common Mistakes:
4. In a parking lot system, if
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same 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. Thread 2 also reads available and proceeds to park.Step 2: Confirm both threads park concurrently causing double-booking
Both threads believe the spot is free and park, leading to inconsistent state and double-booking.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race condition [check]
Common Mistakes:
5. The Restaurant Management System needs to support ElectricCar vehicles that require ChargingSpots. What is the minimum change to the current Java design to add this feature without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses without changing existing classes respects OCP and avoids class explosion.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes allows new vehicle and spot types to integrate without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Hint: Add enums + subclass + external map update -> minimal change [check]
Common Mistakes:
