Skip to main content

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:
  1. VehicleSize: Enum for vehicle sizes
  2. Vehicle: Abstract base class for all vehicles
  3. Motorcycle, Car, Bus: Concrete vehicle implementations
  4. ParkingSpot: Represents a single parking spot
  5. Level: Represents one level of the parking lot
  6. 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 abstract Vehicle class allows treating all vehicle types uniformly while maintaining specific behavior:

Parking Logic

Spot Allocation Strategy

  1. Iterate through levels from bottom to top
  2. For each level, search for appropriate spots
  3. Check compatibility using vehicle.can_fit_in_spot(spot)
  4. For buses, ensure 5 consecutive large spots available
  5. Allocate and mark spots as taken

Multi-Spot Parking (Buses)

Buses require special handling:

Complexity Analysis

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

Potential Improvements

  1. Spot optimization: Algorithm to minimize wasted space (e.g., prefer compact spots for motorcycles)
  2. Reservation system: Allow advance booking of spots
  3. Pricing tiers: Different rates for different spot sizes or locations
  4. Time tracking: Calculate parking duration and fees
  5. Availability display: Show available spots per level
  6. Handicap spots: Special spots with accessibility requirements
  7. Electric vehicle charging: Designate spots with charging stations
  8. Valet mode: Automatic optimal parking selection
  9. Payment integration: Link to payment processing system
  10. Statistics: Track utilization, revenue, peak hours