Bird
Raised Fist0

If the parking lot system needs to support dynamic pricing based on spot location, time, and vehicle type, which design approach best supports this extensibility without modifying existing classes?

hard🎤 Interviewer Follow-up Q15 of Q15
OOP & Design Patterns - Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)
If the parking lot system needs to support dynamic pricing based on spot location, time, and vehicle type, which design approach best supports this extensibility without modifying existing classes?
AUse the Strategy pattern to encapsulate different pricing algorithms and assign them dynamically
BAdd pricing logic directly inside the ParkingSpot class with multiple if-else conditions
CHardcode pricing rules in the ParkingLot class for all spot and vehicle combinations
DCreate separate subclasses of ParkingSpot for each pricing rule variant
Step-by-Step Solution
  1. Step 1: Identify extensibility needs

    Pricing rules vary by multiple factors and may change frequently.
  2. Step 2: Evaluate design options

    Embedding logic in ParkingSpot or ParkingLot leads to rigid, hard-to-maintain code. Subclass explosion is unmanageable.
  3. Step 3: Recognize Strategy pattern benefits

    Strategy encapsulates pricing algorithms, allowing dynamic assignment and easy extension without modifying existing classes.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Strategy pattern supports flexible, maintainable pricing logic [OK]
Quick Trick: Use Strategy pattern to keep pricing logic flexible and decoupled [OK]
Common Mistakes:
MISTAKES
  • Embedding complex logic inside core classes
  • Using inheritance for every pricing variant
Trap Explanation:
PITFALL
  • Options A, C, and D seem straightforward but lead to brittle, unscalable designs.
Interviewer Note:
CONTEXT
  • Tests advanced understanding of design patterns for extensibility in LLD
Master "Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes