π Problem Statement
"Design a parking lot system that supports different vehicle types, multiple entrances and exits, ticketing, and payment."
This question is a favorite in object-oriented design interviews. It challenges you to model real-world entities, their interactions, and support extensible behaviors using OOP principles.
β Requirements
Functional Requirements
- Support multiple types of vehicles: car, bike, truck, electric vehicle
- Different parking spot types: compact, large, handicapped, EV-charging
- Allocate nearest available spot when a vehicle enters
- Issue a ticket on entry and calculate fees on exit
- Allow multiple entrances and exits
- Track available spots per level and type
Non-Functional Requirements
- Scalable and maintainable design
- Extensible: should support new spot types or pricing policies
- Handle concurrency for multiple vehicles entering/exiting simultaneously
π― Key Object-Oriented Concepts
- Inheritance: for vehicle and parking spot hierarchies
- Composition: to associate tickets with vehicles, spots with floors
- Encapsulation: internal state of floors, spots, or payment logic
- Interfaces / Abstract Classes: for payment or pricing strategies
π§± Core Classes and Responsibilities
Vehicle
licensePlate
vehicleType
: car, bike, truck, EV- Can be subclassed:
Car
,Motorcycle
,Truck
,ElectricCar
ParkingSpot
spotId
,spotType
,isOccupied
- Associated with a
floor
and optionally achargingUnit
- Subclasses:
CompactSpot
,LargeSpot
,EVSpot
,HandicappedSpot
ParkingFloor
- Contains multiple parking spots
- Tracks available and occupied counts
- Handles allocation requests from entrances
Ticket
ticketId
,entryTime
,vehicle
,assignedSpot
- Tracks time-in and used for calculating payment
Payment
paymentId
,ticket
,amount
,status
,method
- Subtypes or strategies: credit card, cash, online
EntranceGate
/ ExitGate
- Devices or modules that validate entry/exit
- Trigger ticket generation and fee calculation
ParkingLot
- Aggregates all floors, gates, and configuration logic
- Main system controller
- Supports methods like
assignSpot(vehicle)
,freeSpot(spot)
π Spot Allocation Logic
The system must:
- Match vehicle type to eligible spot types
- Prefer nearest or most optimal spot (e.g., lowest floor)
- Mark spot as occupied and issue a ticket
- Reject if no spot is available for the type
Could be enhanced with:
- Dynamic pricing based on availability
- Preferred parking for monthly users
πΈ Payment and Fee Calculation
- On exit, retrieve ticket and calculate time parked
- Apply pricing policy (e.g., hourly rate, daily cap)
- Integrate with different payment methods
- Mark ticket as paid and free the parking spot
Pricing strategies can vary by spot type, floor, or user type, modeled via strategy pattern.
π Concurrency and Synchronization
- Synchronize spot allocation to avoid double booking
- Use thread-safe collections or locks on critical methods
- Logging and monitoring for entry/exit anomalies
π Extensions and Variations
- Add online reservations and spot pre-booking
- Allow license plate recognition for auto-entry
- Track EV charging unit status
- Add monthly subscriptions or loyalty points
- Generate reports for occupancy or revenue
π§ Interviewer Evaluation Criteria
Area | What Theyβre Looking For |
---|---|
Class Modeling | Clear object responsibilities and clean abstraction |
Inheritance Usage | Proper use of polymorphism and hierarchy for vehicles/spots |
Extensibility | Can the design grow without rewriting core logic? |
Real-world Logic | Does the spot allocation mirror realistic constraints? |
Error Handling | Handling edge cases like full lot, lost tickets, payment fail |
β Summary
Designing a parking lot system tests a wide range of OOD principles, including class hierarchy, encapsulation, interaction between components, and real-time decisions. A robust system supports:
- Flexible spot allocation
- Vehicle diversity
- Ticketing and pricing logic
- Easy future extensions