2Brute2Best
πŸ”₯0
P5

Design a Multi-Floor Parking Lot

Medium High-frequency interview topic Pro OnlyStrategyFactory MethodSingleton
⚑ 30-Second Interview Cheat SheetP5 · 45-Min Design Plan
3 Patterns Combined
1. Scope Boundaries

In a 45-minute LLD interview, the key focus is multi-dimensional strategy decoupling (allocating spots across floors vs calculating dynamic exit fees), entity hierarchy with compatibility constraints (vehicles fitting into equal or larger spot categories), and lifecycle consistency (issuing immutable tickets on entry, reclaiming spots and computing tariffs on exit). Scope to multi-floor management, three spot types (MOTORCYCLE, COMPACT, LARGE), swappable allocation (SpotAllocationStrategy), swappable pricing (PricingStrategy), and ticket tracking. Mention concurrent multi-gate sync and EV charging spot decorators as extensions.

2. Core Noun Entities
Vehicledata-struct
ParkingTicketdata-struct
ParkingSpotstateful-class
SpotAllocationStrategyinterface
3. Hotspot β†’ Pattern
Spot search and allocation algorithm across multiple floors β†’ Strategy (SpotAllocationStrategy interface with LowestFloorFirstStrategy)
Fee calculation on vehicle exit β†’ Strategy (PricingStrategy interface with HourlyPricingStrategy)
Centralized multi-gate coordination β†’ Singleton (ParkingLotManager::getInstance facade)
01

Problem statement & clarifying requirements

Design a Multi-Floor Parking Lot management system. The facility must accommodate diverse vehicle types (Motorcycles, Cars, Trucks) in designated spot sizes across multiple floors. The system must support pluggable spot allocation strategies (such as lowest-floor-first or nearest-to-entrance), swappable pricing models (hourly duration vs flat rates), ticket generation upon entry, and reliable spot reclamation upon exit.

Questions you should ask the interviewer first
β€œWhat are the spot sizing and compatibility rules for different vehicle types?”

Three spot sizes: MOTORCYCLE, COMPACT, and LARGE. Motorcycles can park in any available spot (preferring Motorcycle). Cars fit in Compact or Large spots. Trucks can fit only in Large spots.

β€œHow are spots allocated when multiple eligible spots exist across multiple floors?”

Use the Strategy pattern (SpotAllocationStrategy). The default strategy is LowestFloorFirstStrategy, which fills lower floors completely and exact-fit spots first before exploring higher floors or larger spots.

β€œHow are parking fees computed upon exit?”

Use the Strategy pattern (PricingStrategy). The default model is HourlyPricingStrategy charging differentiated rates (e.g. β‚Ή20/hr for bikes, β‚Ή50/hr for cars, β‚Ή100/hr for trucks) with minimum 1-hour billing.

β€œHow is ticket issuance and spot tracking coordinated?”

A ParkingLotManager singleton facade coordinates the multi-floor facility, using a TicketFactory to stamp tickets on entry and managing active tickets in an in-memory hash map until exit.

02

First, sketch the design yourself

Spend 5 minutes listing the classes, who owns whom, and which interface hides the part that changes.

  1. 1.

    Never hardcode allocation logic or pricing formulas inside ParkingLotManager. Decouple both axes using the Strategy pattern (SpotAllocationStrategy and PricingStrategy).

  2. 2.

    Encapsulate spot dimensions and occupancy state cleanly within ParkingSpot, ensuring thread safety and clear ownership across multi-floor vectors.

  3. 3.

    Use a dedicated TicketFactory (Factory Method) to stamp tickets with auto-incrementing identifiers, vehicle metadata, spot coordinates, and entry timestamps.

03

How to think through this problem (5-step walkthrough)

Step 1 Β· Scope the 45-minute target & core boundary

In a 45-minute LLD interview, the key focus is multi-dimensional strategy decoupling (allocating spots across floors vs calculating dynamic exit fees), entity hierarchy with compatibility constraints (vehicles fitting into equal or larger spot categories), and lifecycle consistency (issuing immutable tickets on entry, reclaiming spots and computing tariffs on exit). Scope to multi-floor management, three spot types (MOTORCYCLE, COMPACT, LARGE), swappable allocation (SpotAllocationStrategy), swappable pricing (PricingStrategy), and ticket tracking. Mention concurrent multi-gate sync and EV charging spot decorators as extensions.

Step 2 Β· Extract candidate nouns (and reject false classes)

Not every noun in the prompt deserves its own class. Separate dumb data structs from stateful managers, and turn trivial nouns into fields or enums.

Vehicle
Value Struct

Value struct bundling licensePlate string and VehicleType enum (MOTORCYCLE, CAR, TRUCK).

ParkingTicket
Value Struct

Immutable value struct linking ticketId, vehicle plate, floor number, spot number, and entry hour.

ParkingSpot
Stateful Class / Manager

Represents an individual physical bay with floor, spot number, SpotType, and occupancy flag (park(), unpark(), isAvailable()).

SpotAllocationStrategy
Abstract Interface

Pluggable strategy contract (findSpot(floors, vehicleType)) decoupling spot search algorithms (lowest floor first, nearest to elevator, round-robin).

PricingStrategy
Abstract Interface

Pluggable tariff contract (calculateFee(ticket, exitHour)) allowing hourly, flat, peak-hour, or weekend pricing without modifying the lot manager.

ParkingLotManager
Stateful Class / Manager

Singleton facade coordinating the floors, holding active tickets in an in-memory hash map, and orchestrating entry/exit flows.

LicensePlate / GateArm
Reject as Class β†’ Field / Method

Trap noun β€” beginners often create separate LicensePlate or GateArm classes. A license plate is easily modeled as a std::string field on Vehicle, and a physical gate arm is a hardware peripheral triggered by parkVehicle returning a valid ticket, not an in-memory business entity.

Step 3 Β· Draft the naive design & spot what breaks

A naive implementation embeds nested loops directly inside ParkingLot::park(vehicle) to find the first open spot and hardcodes fee = hours * 50 inside ParkingLot::unpark(). When management asks to fill Floor 1 before Floor 2, or introduce weekend surge pricing, developers must edit the monolithic ParkingLot class. Furthermore, without spot sizing rules, a naive system parks a motorcycle in a truck bay while a truck is turned away.

Step 4 Β· Map change hotspots to design patterns
Spot search and allocation algorithm across multiple floors→ Strategy (SpotAllocationStrategy interface with LowestFloorFirstStrategy)

Hardcoding floor and bay traversal directly inside ParkingLotManager prevents adding VIP priority, nearest-to-exit heuristics, or load-balancing across floors.

Fee calculation on vehicle exit→ Strategy (PricingStrategy interface with HourlyPricingStrategy)

Embedding fee formulas in the unparking workflow breaks OCP whenever rates change (hourly vs flat rates vs vehicle-tiered multipliers).

Centralized multi-gate coordination→ Singleton (ParkingLotManager::getInstance facade)

Instantiating multiple independent lot managers risks desynchronizing floor counts and assigning the same spot to two vehicles.

Step 5 Β· Lock down C++23 ownership, invariants & concurrency
  • βœ“

    Vehicle-to-spot compatibility invariant: Smaller vehicles may occupy equal or larger spots (canFit(vehicleType, spotType)), but oversized vehicles (e.g. trucks) must never be assigned undersized bays (e.g. motorcycle or compact spots).

  • βœ“

    Ticket uniqueness and spot exclusivity invariant: A ticket is generated with a unique identifier upon entry, and the assigned ParkingSpot remains occupied_ == true until that specific ticket is redeemed at exit.

  • βœ“

    Safe ticket redemption: unparkVehicle validates the existence of the ticket ID in active tickets, frees the spot, computes fees using integer arithmetic, and removes the ticket from active tracking in one cohesive operation.

04

Entities & design patterns used

Brute2Best Pro Content

Unlock Full UML Diagram & C++23 Solution

The complete UML diagram, production-ready C++23 code, follow-up extensions, and design pitfall analysis are part of Brute2Best Pro. All 7 foundation chapters are 100% free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Systems Integration

Related C++ Systems & Algorithmic Foundations

Deep dive into underlying layers