Coursify

Mastering Low Level Design (LLD)

Designing a Parking Lot System

30 mins

Apply your LLD knowledge to design a scalable and robust Parking Lot system, a classic interview problem.

Learning Goals

  • Translate vague requirements into concrete classes and actors.
  • Design a multi-level parking system using composition.
  • Implement flexible pricing using the Strategy pattern.

Case Study 1: The Parking Lot

Designing a Parking Lot is a quintessential LLD challenge. It requires you to balance complex relationships, manage state, and ensure the system is extensible for future requirements (like electric charging spots or different vehicle types).

Step 1: Requirements Gathering

Before drawing a single line, we must define the scope:

  1. Vehicle Types: Car, Truck, Motorcycle, Van.
  2. Spot Types: Small, Medium, Large, Electric.
  3. Capacity: Multiple levels, each with many spots.
  4. Entry/Exit: Multiple gates. Entry issues a ticket; Exit calculates fees and processes payment.
  5. Pricing: Different rates for different vehicle types and durations.

Step 2: Core Actors and Classes

  • Vehicle: Abstract base class for Car, Truck, etc.
  • ParkingSpot: Holds a reference to a Vehicle (if occupied).
  • Level: A collection of ParkingSpots.
  • ParkingLot: A Singleton that manages multiple Levels.
  • Ticket: Stores entry time, spot info, and vehicle info.
  • Gate: Handles entry and exit logic.
  • PricingStrategy: Interface for different billing rules.

Visualizing the static structure

Workflow: Parking a Vehicle

  1. 1
    Step 1

    A vehicle arrives at an entry gate. The Gate identifies the vehicle type (e.g., Motorcycle).

  2. 2
    Step 2

    The ParkingLot iterates through its Levels and asks each level to findAvailableSpot(vehicleType). This is a recursive check for availability.

  3. 3
    Step 3

    If a spot is found, it is marked as 'Occupied'. A Ticket object is created with a unique ID, the current timestamp, and the spot location.

  4. 4
    Step 4

    The ParkingLot updates its global capacity counter. If no spots are available, the gate display shows 'FULL'.

Knowledge Check

Question 1 of 3
Q1Single choice

Why is the Strategy pattern used for pricing in the Parking Lot design?