Notes › System Design
Low-Level Design: Parking Lot
Low-level design (LLD) interviews test whether you can turn requirements into clean classes, relationships and code. The parking lot is the most common LLD question.
How to approach any LLD question
- Clarify requirements — ask, don't assume.
- Identify entities (nouns) and behaviours (verbs).
- Define relationships — IS-A (inheritance), HAS-A (composition).
- Draw the class diagram; use enums for fixed categories.
- Write the core code for the main flows.
- Point out design patterns, SOLID, concurrency and extensions.
1. Requirements
- Multiple floors, each with spots of types SMALL (bike), MEDIUM (car), LARGE (truck).
- A vehicle gets the nearest free spot that fits it.
- Entry gate issues a ticket; exit gate calculates the fee by duration and vehicle type, takes payment, frees the spot.
- Show free spots per floor/type.
- Must handle many gates at once (concurrency).
2. Entities
ParkingLot, Floor, ParkingSpot, Vehicle (Bike, Car, Truck), Ticket, EntryGate/ExitGate, SpotAllocationStrategy, FeeStrategy, Payment.
3. Class diagram
ParkingLot (singleton)
├── List<Floor> ── List<ParkingSpot> (type, isFree, vehicle)
├── SpotAllocationStrategy ← NearestSpotStrategy
├── FeeStrategy ← HourlyFeeStrategy
└── Map<String, Ticket> activeTickets
Vehicle (abstract) ← Bike, Car, Truck (number, type)
Ticket (id, vehicle, spot, entryTime)
Payment (interface) ← UpiPayment, CardPayment, CashPayment
4. Code (Java)
import java.time.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
enum VehicleType { BIKE, CAR, TRUCK }
enum SpotType { SMALL, MEDIUM, LARGE }
abstract class Vehicle {
private final String number;
private final VehicleType type;
protected Vehicle(String number, VehicleType type) { this.number = number; this.type = type; }
public String getNumber() { return number; }
public VehicleType getType() { return type; }
}
class Bike extends Vehicle { Bike(String n) { super(n, VehicleType.BIKE); } }
class Car extends Vehicle { Car(String n) { super(n, VehicleType.CAR); } }
class Truck extends Vehicle { Truck(String n) { super(n, VehicleType.TRUCK); } }
class ParkingSpot {
private final String id;
private final SpotType type;
private Vehicle vehicle; // null = free
ParkingSpot(String id, SpotType type) { this.id = id; this.type = type; }
public boolean isFree() { return vehicle == null; }
public boolean fits(VehicleType v) {
return switch (v) {
case BIKE -> true; // a bike fits anywhere
case CAR -> type != SpotType.SMALL;
case TRUCK -> type == SpotType.LARGE;
};
}
void park(Vehicle v) { this.vehicle = v; }
void free() { this.vehicle = null; }
public String getId() { return id; }
public SpotType getType() { return type; }
}
class Floor {
private final int number;
private final List<ParkingSpot> spots = new ArrayList<>();
Floor(int number) { this.number = number; }
void addSpot(ParkingSpot s) { spots.add(s); }
List<ParkingSpot> getSpots() { return spots; }
long freeCount(SpotType t) { return spots.stream().filter(s -> s.getType() == t && s.isFree()).count(); }
int getNumber() { return number; }
}
class Ticket {
private final String id = UUID.randomUUID().toString();
private final Vehicle vehicle;
private final ParkingSpot spot;
private final LocalDateTime entryTime = LocalDateTime.now();
Ticket(Vehicle v, ParkingSpot s) { vehicle = v; spot = s; }
public String getId() { return id; }
public Vehicle getVehicle() { return vehicle; }
public ParkingSpot getSpot() { return spot; }
public LocalDateTime getEntryTime() { return entryTime; }
}
// ----- strategies (Open/Closed: add new ones without touching ParkingLot) -----
interface SpotAllocationStrategy {
Optional<ParkingSpot> find(List<Floor> floors, VehicleType type);
}
class NearestSpotStrategy implements SpotAllocationStrategy {
public Optional<ParkingSpot> find(List<Floor> floors, VehicleType type) {
for (Floor f : floors) // lowest floor first
for (ParkingSpot s : f.getSpots())
if (s.isFree() && s.fits(type)) return Optional.of(s);
return Optional.empty();
}
}
interface FeeStrategy { double fee(Ticket t, LocalDateTime exit); }
class HourlyFeeStrategy implements FeeStrategy {
private static final Map<VehicleType, Double> RATE =
Map.of(VehicleType.BIKE, 20.0, VehicleType.CAR, 50.0, VehicleType.TRUCK, 100.0);
public double fee(Ticket t, LocalDateTime exit) {
long minutes = Duration.between(t.getEntryTime(), exit).toMinutes();
long hours = Math.max(1, (minutes + 59) / 60); // round up, minimum 1 hour
return hours * RATE.get(t.getVehicle().getType());
}
}
interface Payment { boolean pay(double amount); }
class UpiPayment implements Payment {
public boolean pay(double amount) { System.out.println("UPI paid ₹" + amount); return true; }
}
// ----- the facade -----
class ParkingLot {
private static final ParkingLot INSTANCE = new ParkingLot();
public static ParkingLot get() { return INSTANCE; }
private final List<Floor> floors = new ArrayList<>();
private final Map<String, Ticket> active = new ConcurrentHashMap<>();
private SpotAllocationStrategy allocator = new NearestSpotStrategy();
private FeeStrategy feeStrategy = new HourlyFeeStrategy();
private ParkingLot() {}
public void addFloor(Floor f) { floors.add(f); }
public void setAllocator(SpotAllocationStrategy a) { allocator = a; }
public void setFeeStrategy(FeeStrategy f) { feeStrategy = f; }
// synchronized: two gates must never get the same spot
public synchronized Ticket park(Vehicle v) {
ParkingSpot spot = allocator.find(floors, v.getType())
.orElseThrow(() -> new IllegalStateException("Parking full for " + v.getType()));
spot.park(v);
Ticket t = new Ticket(v, spot);
active.put(t.getId(), t);
return t;
}
public double unpark(String ticketId, Payment payment) {
Ticket t = active.get(ticketId);
if (t == null) throw new IllegalArgumentException("Invalid ticket");
double amount = feeStrategy.fee(t, LocalDateTime.now());
if (!payment.pay(amount)) throw new IllegalStateException("Payment failed");
synchronized (this) {
t.getSpot().free();
active.remove(ticketId);
}
return amount;
}
public void printAvailability() {
for (Floor f : floors)
System.out.printf("Floor %d → small %d, medium %d, large %d%n", f.getNumber(),
f.freeCount(SpotType.SMALL), f.freeCount(SpotType.MEDIUM), f.freeCount(SpotType.LARGE));
}
}
public class Main {
public static void main(String[] args) {
ParkingLot lot = ParkingLot.get();
Floor g = new Floor(0);
g.addSpot(new ParkingSpot("G-S1", SpotType.SMALL));
g.addSpot(new ParkingSpot("G-M1", SpotType.MEDIUM));
g.addSpot(new ParkingSpot("G-L1", SpotType.LARGE));
lot.addFloor(g);
Ticket t = lot.park(new Car("DL01AB1234"));
System.out.println("Car parked at " + t.getSpot().getId()); // G-M1
lot.printAvailability();
System.out.println("Paid: " + lot.unpark(t.getId(), new UpiPayment()));
}
}
5. Design choices to explain
| Choice | Principle / pattern |
|---|---|
Vehicle abstract with Bike/Car/Truck |
inheritance, polymorphism |
SpotAllocationStrategy, FeeStrategy |
Strategy pattern, Open/Closed |
Payment interface |
Dependency Inversion — new methods without changing ParkingLot |
ParkingLot.get() |
Singleton (one lot per system) |
ParkingLot as the single entry point |
Facade |
| Separate Ticket, Spot, Floor classes | Single Responsibility |
| Enums for types | type safety, no magic strings |
Could also mention: Factory to create vehicles from input, Observer to update display boards when a spot changes.
6. Concurrency
Two entry gates could pick the same free spot at the same moment. Options:
synchronizedon the allocate-and-park step (simple; shown above).- Finer locks per floor, or an atomic
compareAndSeton each spot's state for higher throughput. - Per-type queues/sets of free spots (
ConcurrentLinkedQueue) → O(1) allocation instead of scanning.
7. Extensions interviewers ask
- EV charging spots, handicapped spots → new
SpotTypevalues + rules infits(). - Reservations → a
Reservationentity holding a spot for a time window. - Weekend/peak pricing → a new
FeeStrategy, nothing else changes. - Display boards per floor → Observer pattern.
- Persistence → repositories for tickets and spots; the in-memory map becomes a DB.
Interview one-liners
- Start with requirements and entities; code only the core flows.
- Strategy for allocation and pricing, Singleton/Facade for the lot, interface for payments.
- Synchronize spot allocation so two gates never get the same spot.
- Related: SOLID Principles, Four Pillars of OOP, Abstract Class vs Interface.