Elevator System
Multi-Elevator Controller
Design a controller for several elevator cars: each car serves its stops in sweeps (the LOOK rule), and a dispatcher picks the car for each hall call through a swappable strategy.
Overview
A building has several elevator cars and a hall button on every floor. A press inside a car says 'take me to floor 7'. A press in the hall says 'I am on floor 3 and want to go down'. The controller must decide which car answers a hall call, and each car must decide which of its stops to serve next. Both decisions are algorithms, and both must stay correct while buttons are pressed from many places at once.
This is one of the harder LLD problems because it mixes a state machine (a car is idle, moving or standing with its doors open), a scheduling rule (in which order to serve stops) and a dispatch rule (which car to send). The trap is to over-build: threads per car, an event bus, a dozen classes. A strong answer keeps a car to a small object with one method that advances one tick, and drives it from a deterministic test.
The scheduling rule interviewers expect is borrowed from disk scheduling. A car keeps moving in its current direction while any stop lies ahead, and reverses only when none does. Requests behind it wait for the next sweep. That rule is called LOOK, and it stops the car from zig-zagging.
Scoping the problem
Before writing code, ask these questions; each answer changes the design a little.
- How many cars and floors? It decides whether dispatch matters at all. The usual answer is a few cars and tens of floors.
- Do hall buttons have a direction? A directed call lets the dispatcher avoid a car heading the wrong way. Yes, up and down.
- Is time simulated in ticks or real? Ticks make the design testable, and real time is a thin wrapper. Use ticks: one floor or one door action per tick.
- Weight or passenger limits? A full car should skip hall calls. Treat it as a follow-up, not the core.
- Express cars or zones? They change which cars may serve which floors. Also a follow-up.
- Who presses buttons at once? The cabin, every hall and the dispatcher, so the request set needs a lock. Expect many threads.
Functional requirements
- Several cars serve floors 0 to N-1; each car keeps a set of stops from cabin buttons and dispatched hall calls
- A car serves its stops with the LOOK rule: keep the direction while a stop lies ahead, reverse when none does
- step() advances one tick: move one floor, open the doors at a stop, or close them
- hall_call(floor, direction) picks a car through a swappable dispatch strategy and adds the floor to its stops
- A request for a floor outside the building is refused
- Each car exposes its floor, direction and state for displays
Non-functional requirements
- Deterministic: time is a tick, so every scenario is a plain test with no sleeps
- Safe for many threads pressing buttons on the same car: one lock per car around its stop set
- A new dispatch rule is a new class; the cars do not change
- A car does O(stops) work per tick on a small set; a sorted structure would make it O(log n) (see the questions)
Interview questions
- Why LOOK rather than SCAN or first-come-first-served?
- How would you add express cars that skip floors, or zones?
- A thread per car, or one loop that steps every car?
- Your car stores stops in a set and calls max() and any() on it. Is that acceptable?
- How do you handle a weight limit or a full car?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio