LESSON 11.2
Walkthrough: parking lot
End-to-end design and implementation of a multi-floor parking lot system: entity modeling, spot allocation strategy, tickets, and fee calculation.
6 min read
The Parking Lot is the most frequently asked Machine Coding question worldwide. Let us walk through how to design, model, and code a production-ready solution under interview conditions.
1. Requirements & Scope
- Floors & Spots: Multiple floors, each with numbered spots designated for
MOTORCYCLE,COMPACT(Car), andLARGE(Truck). - Core Operations:
park(vehicle)-> issues aParkingTicket.unpark(ticket)-> frees the spot and calculates fee.
- Extensibility: Pluggable pricing strategy (e.g. flat hourly vs vehicle-tiered) and pluggable spot assignment strategy.
2. Entity Diagram
Laying out the diagram…
3. Implementation
// Sizing and Entity Types
const SpotType = { MOTORCYCLE: "MOTORCYCLE", COMPACT: "COMPACT", LARGE: "LARGE" };
class Vehicle {
constructor(plate, type) {
this.plate = plate;
this.type = type;
}
}
class ParkingSpot {
#vehicle = null;
constructor(id, floorNumber, spotType) {
this.id = id;
this.floorNumber = floorNumber;
this.spotType = spotType;
}
canFit(vehicle) {
if (this.#vehicle !== null) return false;
if (vehicle.type === SpotType.MOTORCYCLE) return true;
if (vehicle.type === SpotType.COMPACT) {
return this.spotType === SpotType.COMPACT || this.spotType === SpotType.LARGE;
}
return this.spotType === SpotType.LARGE;
}
assign(vehicle) {
if (!this.canFit(vehicle)) throw new Error("Spot cannot accommodate vehicle");
this.#vehicle = vehicle;
}
vacate() { this.#vehicle = null; }
get isOccupied() { return this.#vehicle !== null; }
}
class ParkingTicket {
constructor(id, vehiclePlate, spot) {
this.id = id;
this.vehiclePlate = vehiclePlate;
this.spot = spot;
this.entryTime = Date.now();
}
}
// Strategy: Hourly fee calculation
class FlatRatePricingStrategy {
calculate(ticket, exitTime) {
const hours = Math.max(1, Math.ceil((exitTime - ticket.entryTime) / (1000 * 60 * 60)));
return hours * 10.0; // $10/hour
}
}
// Parking Lot Coordinator
class ParkingLot {
#spots = [];
#activeTickets = new Map();
#pricingStrategy;
constructor(spots, pricingStrategy = new FlatRatePricingStrategy()) {
this.#spots = spots;
this.#pricingStrategy = pricingStrategy;
}
park(vehicle) {
const availableSpot = this.#spots.find((s) => s.canFit(vehicle));
if (!availableSpot) throw new Error("Parking full for " + vehicle.type);
availableSpot.assign(vehicle);
const ticket = new ParkingTicket(`TKT-${Date.now()}`, vehicle.plate, availableSpot);
this.#activeTickets.set(ticket.id, ticket);
return ticket;
}
unpark(ticketId, exitTime = Date.now()) {
const ticket = this.#activeTickets.get(ticketId);
if (!ticket) throw new Error("Invalid ticket ID");
const fee = this.#pricingStrategy.calculate(ticket, exitTime);
ticket.spot.vacate();
this.#activeTickets.delete(ticketId);
return { fee, vehiclePlate: ticket.vehiclePlate };
}
}
// Verification Test
const spots = [
new ParkingSpot("F1-01", 1, SpotType.COMPACT),
new ParkingSpot("F1-02", 1, SpotType.LARGE)
];
const lot = new ParkingLot(spots);
const car = new Vehicle("KA-05-AB-1234", SpotType.COMPACT);
const ticket = lot.park(car);
console.assert(spots[0].isOccupied === true);
// Unpark after 2 hours simulated:
const receipt = lot.unpark(ticket.id, ticket.entryTime + 2 * 3600 * 1000);
console.assert(receipt.fee === 20.0);
console.assert(spots[0].isOccupied === false);
console.log("Unparked successfully. Receipt fee:", receipt.fee);public class Example {
enum SpotType { MOTORCYCLE, COMPACT, LARGE }
record Vehicle(String plate, SpotType type) {}
static final class ParkingSpot {
final String id;
final SpotType type;
private Vehicle vehicle;
ParkingSpot(String id,
JavaScript · 101 lines
UTF-8Evaluation Checklist
- Did you handle capacity limits gracefully with clean error messages?
- Are vehicle types matched to spots using polymorphic rules rather than hardcoded string equality?
- Is pricing extracted into an interchangeable Strategy?
Try it
Parking Lot LLD
Implement a floor-level display board showing real-time available spot counts per vehicle type.