PrepZone Logo
PrepZone

Design a Parking Lot System

CampusOS parking — spots, tickets, pricing by vehicle type.

Why this matters

  • One of the most common LLD prompts — patterns for allocation, ticketing, and pricing appear everywhere.
  • Tests OOP, Strategy, and facade coordination in a single story.
  • CampusOS parking is the reference machine problem for the rest of the track.
ParkingLotentry facade
ParkingFloorcollection of spots
Ticketissued on entry
Vehiclecar, bike, truck

ParkingSpot links Vehicle when occupied; Strategy picks spot by vehicle type.

ParkingLot owns floors; each spot knows its size and occupant.

Requirements

Functional

  • Multiple spot types: handicapped, compact, large, motorcycle
  • Vehicle types: car, truck, van, motorcycle — each maps to compatible spots
  • Entry issues a ticket; exit calculates hourly fee and frees the spot
  • Display board shows availability per spot type; show "FULL" when capacity reached
  • Multiple entry and exit gates share one lot

Non-functional

  • In-process, single JVM for this article
  • Spot assignment must be consistent under concurrent entry at two gates
  • Pricing rule should be replaceable (Strategy) without editing ParkingLot

Core classes

ClassResponsibility
ParkingLotFacade — park, exit, availability
ParkingFloorOwns a collection of spots
ParkingSpotType, occupied flag, park/vacate
VehicleType enum + identifier
ParkingTicketEntry time, spot reference, fee calculation
SpotAssignmentStrategyPicks next free compatible spot
DisplayBoardPublishes availability snapshot
  • ParkingLot

    ResponsibilityFacade — park, exit, availability
  • ParkingFloor

    ResponsibilityOwns a collection of spots
  • ParkingSpot

    ResponsibilityType, occupied flag, park/vacate
  • Vehicle

    ResponsibilityType enum + identifier
  • ParkingTicket

    ResponsibilityEntry time, spot reference, fee calculation
  • SpotAssignmentStrategy

    ResponsibilityPicks next free compatible spot
  • DisplayBoard

    ResponsibilityPublishes availability snapshot

Entry and exit flow

  1. Entry — vehicle arrives → strategy finds spot → spot.occupy() → ticket issued with timestamp
  2. Exit — ticket presented → fee = hours × rate → payment → spot.vacate() → board updated
Java
public enum VehicleType { CAR, TRUCK, VAN, MOTORCYCLE }
public enum SpotType { HANDICAPPED, COMPACT, LARGE, MOTORCYCLE }

public final class ParkingSpot {
    private final SpotType type;
    private boolean occupied;

    public boolean canFit(VehicleType vehicle) {
        return compatibility.get(vehicle).contains(type);
    }

    public void park() {
        if (occupied) throw new IllegalStateException("Spot already taken");
        occupied = true;
    }

    public void vacate() { occupied = false; }
}

public final class ParkingLot {
    private final List<ParkingFloor> floors;
    private final SpotAssignmentStrategy strategy;
    private final DisplayBoard board;

    public Optional<ParkingTicket> park(Vehicle vehicle) {
        return strategy.findSpot(floors, vehicle)
            .map(spot -> {
                spot.park();
                var ticket = new ParkingTicket(vehicle, spot, Instant.now());
                board.refresh(countAvailability());
                return ticket;
            });
    }
}

Extensions

  • Electric charging spots — new SpotType + compatibility row
  • Dynamic pricing — swap PricingStrategy at exit
  • Multi-lot campus — one ParkingLot per physical lot behind a registry

Quick recall

Everything you need if you only revisit this box.

  1. ParkingLot is the facade; floors own spots
  2. Strategy selects compatible free spot
  3. Ticket binds vehicle, spot, entry time
  4. DisplayBoard observes availability changes
  5. Payment and pricing are separate collaborators

CampusOS extension

At campus scale, ticket and payment events flow to a central billing service; availability may be cached at each gate. See System Design articles on caching and idempotent payments when you outgrow one JVM.

Test yourself

Answer these before moving on — recall is what makes it stick.