Which design approach best uses the Parking Strategy Pattern to handle this complexity?
hard📝 Trade-off Q15 of Q15
LLD - Design — Parking Lot System
You want to design a parking system that supports multiple vehicle types (car, bike, truck) and different parking strategies (nearest, random, reserved). Which design approach best uses the Parking Strategy Pattern to handle this complexity?
ACreate separate parking lot classes for each vehicle type and hardcode the strategy inside each
BUse a single ParkingLot class with a strategy interface; implement different strategies and select based on vehicle type at runtime
CStore all vehicles in one list and assign spots randomly without strategy pattern
DUse global variables to track vehicle types and apply strategies in procedural code
Step-by-Step Solution
Solution:
Step 1: Identify need for flexibility and scalability
Supporting multiple vehicle types and strategies requires flexible design without code duplication.
Step 2: Apply strategy pattern with runtime selection
Using one ParkingLot class with strategy interface allows swapping strategies dynamically based on vehicle type.
Step 3: Evaluate other options
Create separate parking lot classes for each vehicle type and hardcode the strategy inside each duplicates code, C ignores strategy pattern, D uses poor global state management.
Final Answer:
Use a single ParkingLot class with a strategy interface; implement different strategies and select based on vehicle type at runtime -> Option B
Quick Check:
Strategy pattern + runtime selection = best design [OK]
Quick Trick:Use one class + strategy interface + runtime choice [OK]
Common Mistakes:
MISTAKES
Duplicating classes per vehicle type
Ignoring strategy pattern benefits
Using global variables instead of OOP
Master "Design — Parking Lot System" in LLD
9 interactive learning modes - each teaches the same concept differently