Bird
Raised Fist0

In a parking lot system designed to handle various vehicle categories and flexible fee structures, which design principle or pattern most effectively enables adding new vehicle categories and pricing models without altering existing code?

hard🔁 Replacement Q9 of Q15
OOP & Design Patterns - Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)
In a parking lot system designed to handle various vehicle categories and flexible fee structures, which design principle or pattern most effectively enables adding new vehicle categories and pricing models without altering existing code?
AHardcode pricing logic inside the Vehicle classes to tightly couple pricing with vehicle behavior.
BImplement all vehicle types and pricing rules directly within the ParkingLot class for centralized control.
CUse inheritance to create subclasses for each vehicle and pricing type, modifying base classes as needed.
DUse the Strategy pattern combined with interfaces for vehicles and pricing to allow independent extension.
Step-by-Step Solution
Solution:
  1. Step 1: Identify extensibility needs

    The system must allow new vehicle types and pricing rules without modifying existing classes.
  2. Step 2: Apply Strategy pattern

    Strategy pattern encapsulates algorithms (pricing) and behaviors (vehicle types) behind interfaces, enabling independent extension.
  3. Step 3: Avoid tight coupling

    Centralizing logic or hardcoding pricing reduces flexibility and violates Open/Closed Principle.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Extensibility without modification? Strategy pattern fits perfectly. [OK]
Quick Trick: Strategy pattern enables flexible extension without code changes [OK]
Common Mistakes:
MISTAKES
  • Choosing inheritance leads to rigid, hard-to-maintain code.
  • Centralizing logic in ParkingLot class reduces modularity.
  • Hardcoding pricing in Vehicle classes couples unrelated concerns.
Trap Explanation:
PITFALL
  • Centralizing logic or inheritance seems simpler but reduces flexibility and violates design principles.
Interviewer Note:
CONTEXT
  • Tests understanding of design patterns and principles for extensible system design.
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