2Brute2Best
πŸ”₯0
P13

Design a Multi-Car Elevator System

Hard High-frequency interview topic Pro OnlyStateStrategyCommand
⚑ 30-Second Interview Cheat SheetP13 · 45-Min Design Plan
3 Patterns Combined
1. Scope Boundaries

In a 45-minute LLD interview, the key focus is modeling physical elevator kinematics, directional scheduling, and fleet dispatching. Scope the problem to: a commercial building with M elevator cars and N floors, handling corridor hall calls (HallRequest with floor and direction) and in-cabin button presses (CabinRequest with target floor), executing the directional LOOK sweep algorithm, and dispatching cars via cost heuristics. Discrete simulation ticks advance car positions.

2. Core Noun Entities
ElevatorStateinterface
ElevatorCarstateful-class
ElevatorCommandinterface
DispatchStrategyinterface
3. Hotspot β†’ Pattern
Car operational motion and transition logic (Idle vs Moving Up vs Moving Down) β†’ State Pattern (ElevatorState: IdleState, MovingUpState, MovingDownState)
Fleet dispatching policy for external hall requests β†’ Strategy Pattern (DispatchStrategy with NearestDirectionalDispatcher)
Request decoupling between user input panels and physical cars β†’ Command Pattern (ElevatorCommand: HallRequest and CabinRequest)
01

Problem statement & clarifying requirements

Design a multi-car elevator system for a high-rise commercial building. The design must model the physical state machine of each elevator car (Idle, MovingUp, MovingDown), encapsulate hall and cabin button presses as executable commands, and efficiently assign cars to requests using the LOOK elevator scheduling algorithm and directional dispatch strategies.

Questions you should ask the interviewer first
β€œHow are button requests classified in the elevator system?”

Requests are split into Hall Requests (pressed from a floor corridor with a source floor and desired direction Up/Down) and Cabin Requests (pressed inside an elevator cabin specifying a destination floor).

β€œWhat scheduling algorithm dictates how an elevator car traverses floors?”

Cars follow the LOOK algorithm (elevator sweep): a car continues servicing all requests in its current direction until no targets remain ahead, before reversing direction or returning to Idle.

β€œHow does the central dispatcher pick the best car for an external hall request?”

A DispatchStrategy calculates a cost score for each car based on current floor distance, direction alignment (e.g. car is already moving towards the floor in the requested direction), and idle availability.

β€œHow is simulation time progressed?”

The system advances in discrete simulation ticks (step()), where each tick advances moving cars by one floor, opens doors upon reaching destination targets, and transitions state machines.

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.

    State Pattern (a4) for car movement: State transitions between IdleState, MovingUpState, and MovingDownState have distinct movement rules and target scanning. Placing them in polymorphic state classes eliminates fragile nested switch statements inside ElevatorCar.

  2. 2.

    Command Pattern (b4) for decoupled requests: Both hall calls and cabin calls are encapsulated as ElevatorCommand objects (HallRequest and CabinRequest), decoupling request producers (hallway buttons, touchscreen panels) from target car queues.

  3. 3.

    Strategy Pattern (a1) for pluggable dispatching: Dispatching logic evolves independently from physical car mechanics. Isolating NearestDirectionalDispatcher behind DispatchStrategy allows switching to Odd/Even or Zone dispatching without modifying elevator car internals.

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 modeling physical elevator kinematics, directional scheduling, and fleet dispatching. Scope the problem to: a commercial building with M elevator cars and N floors, handling corridor hall calls (HallRequest with floor and direction) and in-cabin button presses (CabinRequest with target floor), executing the directional LOOK sweep algorithm, and dispatching cars via cost heuristics. Discrete simulation ticks advance car positions.

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.

ElevatorState
Abstract Interface

Polymorphic State contract (IdleState, MovingUpState, MovingDownState) encapsulating directional motion, destination arrival, and LOOK sweep transitions without switch-case sprawl.

ElevatorCar
Stateful Class / Manager

Physical elevator cabin holding car ID, current floor, door state, target floor sets (std::set<int>), and a std::unique_ptr<ElevatorState>.

ElevatorCommand
Abstract Interface

Command pattern contract (HallRequest, CabinRequest) encapsulating external corridor calls and internal cabin presses executed against target cars.

DispatchStrategy
Abstract Interface

Strategy contract (NearestDirectionalDispatcher) evaluating fleet cars and selecting the optimal car for hall requests based on proximity and motion alignment.

ElevatorSystem
Stateful Class / Manager

Central controller managing the fleet of cars, delegating hall calls to the dispatcher, and ticking the discrete simulation loop.

FloorNumber / DoorMotor
Reject as Class β†’ Field / Method

Trap noun β€” creating dedicated classes for FloorNumber or DoorMotor overengineers the domain. A floor is a primitive int with range checks, and door state is an enum class DoorState { OPEN, CLOSED } toggled by openDoors() and closeDoors() member functions.

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

A beginner models an elevator with a giant while(true) loop full of nested switch statements (switch (status) { case MOVING: switch (dir) { ... } }) inside ElevatorCar. When adding door safety checks, reversing at terminal floors, or changing dispatching logic (e.g. from nearest car to odd/even zoning), code becomes an entangled mess of flag variables (isMoving, isGoingUp, doorsOpen) where state corruption easily leads to simulated crashes or infinite loops.

Step 4 Β· Map change hotspots to design patterns
Car operational motion and transition logic (Idle vs Moving Up vs Moving Down)β†’ State Pattern (ElevatorState: IdleState, MovingUpState, MovingDownState)

Nested conditionals checking directional flags make adding maintenance modes, emergency stops, or reverse sweeps fragile and bug-prone.

Fleet dispatching policy for external hall requests→ Strategy Pattern (DispatchStrategy with NearestDirectionalDispatcher)

Hardcoding car assignment inside the central controller prevents adapting to morning up-peak rushes, evening down-peaks, or zoned building layouts.

Request decoupling between user input panels and physical cars→ Command Pattern (ElevatorCommand: HallRequest and CabinRequest)

Directly mutating car queues from corridor buttons couples physical button panels to car internals and makes request queuing or logging difficult.

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

    LOOK sweep invariant: An elevator moving in direction D must service all requested stops ahead along D before reversing direction or transitioning to IdleState.

  • βœ“

    Door safety invariant: Elevator cars can only move between floors when doors are strictly CLOSED. Upon reaching a target floor, movement halts, doors open to service passengers, the target floor is cleared from the target set, and doors close before the next tick.

  • βœ“

    Target set consistency: Target floors are stored in sorted collections (std::set<int>) so directional searches (lower_bound / upper_bound) execute in O(log F) time without duplicate stops.

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