Design a Parking Lot

Medium
Real World SystemsSingletonFactoryStrategy

Problem Statement

Design a parking lot system that can accommodate different types of vehicles. The system should be able to:

  • Support multiple levels/floors
  • Handle different vehicle types (Motorcycle, Car, Truck)
  • Have different parking spot sizes (Small, Medium, Large)
  • Issue and process parking tickets
  • Calculate parking fees based on time

Design Requirements

Entities to identify:

  • ParkingLot — Main system entry point (Singleton)
  • Level — A floor in the parking lot
  • ParkingSpot — Individual spot with size and availability
  • Vehicle — Abstract vehicle with subtypes
  • ParkingTicket — Issued on entry, used on exit
  • FeeCalculator — Strategy for calculating fees

Key Behaviors:

  • parkVehicle(vehicle) → returns a ParkingTicket or null if full
  • unparkVehicle(ticket) → returns the fee amount
  • getAvailableSpots() → count of available spots
  • Vehicles can only park in appropriate-sized spots (Motorcycle → Small/Medium/Large, Car → Medium/Large, Truck → Large only)

!Constraints

- A parking lot has 1–5 levels - Each level has 10–100 spots - Spot sizes: Small (bikes), Medium (cars), Large (trucks/buses) - Parking fee: $2/hour for Small, $4/hour for Medium, $6/hour for Large - A vehicle must park in the smallest available spot that fits it - The system must be thread-safe (assume concurrent access)
Output

Run your code to see output here.