Skip to content
LLD Learn/Case Studies: Physical Systems
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Design a Parking Lot

5 min read

You'll learn to

  • -Walk the five-step framework on a real prompt: clarify, find the classes, protect the shared resource, then extend
  • -Model vehicles, spots and levels so a new vehicle type is new code rather than edited code
  • -Make spot assignment one atomic step, and see how the choice of search rule changes who gets parked

"Design a parking lot" is one of the most-asked LLD prompts because it packs several fundamentals into one small problem. Different kinds of vehicles and spots call for a class hierarchy. Handing out spots is a concurrency-sensitive step, because many cars may arrive at once. And there are natural follow-ups, such as adding payment or a second entrance. Working through it ties together most of what this course has covered so far.

Step 1: Clarify Requirements

Before drawing anything, ask the questions that change the design. The answers decide which classes you need.

  • -Which vehicle types? Motorcycle, car and bus may each need a different size of spot.
  • -Is there more than one level or floor?
  • -Do we track payment, or only entry, exit and spot assignment?
  • -What happens when the lot is full? Do we support reserving a spot in advance, or only walk-up parking?

Assume the answers are: three vehicle types (motorcycle, car, bus), several levels, spots of different sizes on each level, and payment out of scope for this first pass. Payment is a natural place for the interviewer to follow up later.

Step 2: Identify the Classes

Underline the nouns in the requirements: vehicle, spot, level, lot, ticket. Each becomes a class. A vehicle type and a spot size are small fixed sets of named choices, so they are enums, which is Python's way to say "one of these exact values". The sketch shows the shape. A spot can fit a vehicle if its size is at least what the vehicle needs, which lets a motorcycle use a larger spot when the small ones are full.

The core class shape
from enum import Enum

class VehicleType(Enum):
    MOTORCYCLE = "motorcycle"
    CAR = "car"
    BUS = "bus"

class Vehicle(ABC):
    def __init__(self, license_plate: str):
        self.license_plate = license_plate
    @property
    @abstractmethod
    def type(self) -> VehicleType: ...

class Car(Vehicle):
    @property
    def type(self) -> VehicleType: return VehicleType.CAR

class SpotSize(Enum):
    SMALL = 1     # motorcycles only
    MEDIUM = 2    # cars (and motorcycles)
    LARGE = 3     # buses (and cars, and motorcycles)

class ParkingSpot:
    def __init__(self, spot_id: str, size: SpotSize):
        self.spot_id = spot_id
        self.size = size
        self.vehicle: Vehicle | None = None

    def can_fit(self, vehicle: Vehicle) -> bool:
        required = {VehicleType.MOTORCYCLE: SpotSize.SMALL,
                    VehicleType.CAR: SpotSize.MEDIUM,
                    VehicleType.BUS: SpotSize.LARGE}[vehicle.type]
        return self.size.value >= required.value

    def is_free(self) -> bool:
        return self.vehicle is None

Step 3: Make Assignment One Atomic Step

This is the part a naive solution gets wrong. Finding a free spot and assigning it must be one atomic operation, meaning no other thread can slip in between the two halves. It is exactly the check-then-act race from the Concurrency module. The ParkingLevel owns its spots and a lock around the search-and-assign step.

Atomic search-and-assign, not a separate find() then assign()
import threading

class ParkingLevel:
    def __init__(self, spots: list[ParkingSpot]):
        self._spots = spots
        self._lock = threading.Lock()

    def park(self, vehicle: Vehicle) -> ParkingSpot | None:
        with self._lock:                              # find AND assign, atomically
            for spot in self._spots:
                if spot.is_free() and spot.can_fit(vehicle):
                    spot.vehicle = vehicle
                    return spot
            return None                                # this level is full for this vehicle type

class ParkingLot:
    def __init__(self, levels: list[ParkingLevel]):
        self._levels = levels

    def park_vehicle(self, vehicle: Vehicle) -> "Ticket | None":
        for level in self._levels:
            spot = level.park(vehicle)
            if spot:
                return Ticket(vehicle, spot)
        return None                                    # lot is full

Step 4: Ticket and Extensibility

A Ticket ties a vehicle to the spot it was given, plus an entry time. It is the natural place to add payment later, as duration times hourly rate, without touching ParkingLevel or ParkingSpot. The design has already absorbed the two most common follow-ups. "Add a new vehicle type" means one new enum value and one new Vehicle subclass. "Add payment" means extending Ticket. Neither touches the atomic assignment logic.

Step 5: Run It, and Notice a Design Choice

The code below is a compact, runnable version. It also exposes a choice hiding inside the search loop. The level has three spots: large, medium and small, listed in that order. Three vehicles arrive: a car, a motorcycle and a bus. With first-fit, a vehicle takes the first free spot that fits. With best-fit, it takes the smallest free spot that fits.

Press Run: the same three vehicles, two search rules, different outcomes

With first-fit, the car grabs the only large spot, so the bus has nowhere to go even though the lot has a free medium and a free small spot. With best-fit, everyone fits. The point is not that one rule is always right. It is that you spotted a design decision the problem hides, and you can say why you chose one.

A strong closing move: mention that can_fit uses ">=", so a motorcycle can fall back to a medium or large spot when small ones are full, and that best-fit stops that fallback from stranding larger vehicles. It shows you thought about how well the lot is used, not just whether the code is correct.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Forge Level 35: Parking Lot System asks you to implement exactly this - a ParkingLot that assigns a vehicle to the smallest available spot that fits it (never a smaller one) and frees the spot on exit, graded by running your actual code against real test cases.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!