Design Parking Lot (Low-Level Design)
Park vehicles in the right size of spot, issue tickets and charge on exit. Focus: Modelling objects, matching rules and a pricing policy.
| Difficulty | EASY |
|---|---|
| Design focus | Modelling objects, matching rules and a pricing policy |
| Automated grading | 3 public + 13 hidden tests (16 total) |
| Phase 2 follow-up | Electric vehicles |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Build a parking lot with spots of three sizes on several levels.
Motorcycles fit any spot, cars fit MEDIUM or LARGE spots, trucks only fit LARGE spots. A vehicle always gets the smallest spot it fits; among those, the lowest level, then the spot listed first.
Time is given in minutes. The fee is per started hour, with a minimum of one hour: motorcycle 1, car 2, truck 5.
Functional requirements
- park(plate, kind, time) returns a ticket; it throws if no spot fits or the plate is already parked.
- leave(ticketId, time) frees the spot and returns the fee; it throws for an unknown or already used ticket.
- freeSpots(size?) counts free spots, of one size or all.
- findVehicle(plate) returns the spot id, or null.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
type VehicleKind = "MOTORCYCLE" | "CAR" | "TRUCK";
type SpotSize = "SMALL" | "MEDIUM" | "LARGE";
interface Ticket {
id: string;
plate: string;
spotId: string;
entryTime: number; // minutes
}
class ParkingLot {
constructor(spots: { id: string; size: SpotSize; level: number }[]) {}
park(plate: string, kind: VehicleKind, time: number): Ticket {
throw new Error("Not implemented");
}
/** Returns the fee */
leave(ticketId: string, time: number): number {
throw new Error("Not implemented");
}
freeSpots(size?: SpotSize): number {
throw new Error("Not implemented");
}
findVehicle(plate: string): string | null {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 13 hidden edge-case tests in a sandbox:
A car parks in a medium spot
var lot = new ParkingLot([{ id: "S1", size: "SMALL", level: 0 }, { id: "M1", size: "MEDIUM", level: 0 }]);
var t = lot.park("CAR-1", "CAR", 0);
assertEqual(t.spotId, "M1");
assertEqual(lot.findVehicle("CAR-1"), "M1");
Fee is per started hour
var lot = new ParkingLot([{ id: "M1", size: "MEDIUM", level: 0 }]);
var t = lot.park("CAR-1", "CAR", 0);
assertEqual(lot.leave(t.id, 61), 4);
A truck needs a large spot
var lot = new ParkingLot([{ id: "M1", size: "MEDIUM", level: 0 }]);
assertThrows(function () { lot.park("TRK-1", "TRUCK", 0); });
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- Pricing lives in its own policy (an interface or class), not inside the lot: Prices change more often than parking rules. A separate pricing policy can change, or gain an EV rate, without touching how spots are found.
- Vehicle kinds and spot sizes are named types, not loose strings: Named types let the compiler catch a typo like "CRA" and show every place a new kind must be handled.
- Spots do not know about tickets or prices: A spot only needs to know its size and whether it is taken. Keeping it small makes it reusable and easy to test.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 16 tests, you unlock the change request: Electric vehicles.
Relevant low-level design lessons
From a problem to classes
Turn a problem statement into a first list of classes and methods.
One class, one job
Spot a class that does too much and split it.
Hide state behind methods
Protect an object's rules by keeping its data private.
More low-level design problems
Design Shopping Cart
Already solved, with comments: add items, apply tier discounts, then coupons. Run it and submit it to see how grading works.
Design Tic-Tac-Toe (N × N)
Any board size, with a win check that only looks around the last move.
Design Vending Machine
Take coins, sell products, give change, and refuse what a state does not allow.
Design Snake and Ladder
A board game where the board, the players and the rules for a turn are separate pieces.