Problem Statement
Design a parking lot system that manages multiple levels, different vehicle types, and various parking spot sizes with intelligent space allocation.Constraints and Assumptions
- Vehicle types: Motorcycle, Car, Bus
- Spot allocation:
- Motorcycle spot → Motorcycle only
- Compact spot → Motorcycle, Car
- Large spot → Motorcycle, Car
- Bus requires 5 consecutive large spots
- Multi-level: Parking lot has multiple levels
- Inputs are valid: No validation needed
Design Overview
The parking lot system uses a hierarchy of classes to model the physical structure and vehicle types:- VehicleSize: Enum for vehicle sizes
- Vehicle: Abstract base class for all vehicles
- Motorcycle, Car, Bus: Concrete vehicle implementations
- ParkingSpot: Represents a single parking spot
- Level: Represents one level of the parking lot
- ParkingLot: Main orchestrator for the entire system
This design uses the Composite pattern to build a hierarchy (ParkingLot → Levels → Spots) and the Strategy pattern to allow different vehicles to determine their own parking requirements.
Implementation
VehicleSize Enumeration
Vehicle Hierarchy
Abstract base class with common vehicle functionality:Concrete Vehicle Classes
Each vehicle type implements its own parking logic:ParkingLot Class
Top-level orchestrator:Level Class
Manages one level of parking:ParkingSpot Class
Represents individual parking spots:Key Design Patterns
Composite Pattern
Hierarchical structure for the parking system:Strategy Pattern
Each vehicle determines its own parking requirements:- Motorcycle: Can fit anywhere (
can_fit_in_spot()always returns True) - Car: Requires compact or large spots
- Bus: Requires 5 consecutive large spots
Polymorphism
The abstractVehicle class allows treating all vehicle types uniformly while maintaining specific behavior:
Parking Logic
Spot Allocation Strategy
- Iterate through levels from bottom to top
- For each level, search for appropriate spots
- Check compatibility using
vehicle.can_fit_in_spot(spot) - For buses, ensure 5 consecutive large spots available
- Allocate and mark spots as taken
Multi-Spot Parking (Buses)
Buses require special handling:Complexity Analysis
| Operation | Time Complexity | Notes |
|---|---|---|
| park_vehicle() | O(n × m) | n levels, m spots per level |
| find_available_spot() | O(m) | m spots on a level |
| can_fit_in_spot() | O(1) | Simple boolean check |
| clear_spots() | O(k) | k spots taken by vehicle |
For buses, finding 5 consecutive spots requires scanning the level, which is O(m) where m is the number of spots. The worst case is when we scan all levels and all spots, resulting in O(n × m) time complexity.
Design Considerations
Advantages
- Flexible: Easy to add new vehicle types or spot sizes
- Encapsulation: Each class manages its own state and behavior
- Scalable: Can add unlimited levels and spots
- Type-safe: Abstract methods enforce implementation of required behavior
Spot Size Flexibility
| Vehicle | Motorcycle Spot | Compact Spot | Large Spot |
|---|---|---|---|
| Motorcycle | ✓ | ✓ | ✓ |
| Car | ✗ | ✓ | ✓ |
| Bus | ✗ | ✗ | ✓ (5 consecutive) |
Potential Improvements
- Spot optimization: Algorithm to minimize wasted space (e.g., prefer compact spots for motorcycles)
- Reservation system: Allow advance booking of spots
- Pricing tiers: Different rates for different spot sizes or locations
- Time tracking: Calculate parking duration and fees
- Availability display: Show available spots per level
- Handicap spots: Special spots with accessibility requirements
- Electric vehicle charging: Designate spots with charging stations
- Valet mode: Automatic optimal parking selection
- Payment integration: Link to payment processing system
- Statistics: Track utilization, revenue, peak hours
