Designing an Elevator System
Difficulty: Intermediate Patterns: Strategy, State, Singleton, Command Asked at: Amazon, Uber, PhonePe, Google, Flipkart
The elevator problem looks like it’s about physics but it’s really about scheduling: given a set of requests, which car goes where, and in what order does a car serve the floors it’s committed to? A strong answer separates three concerns — the car (moves and tracks state), the dispatcher (picks which car handles a request), and the movement strategy (what order a car serves its stops). Bundle those together and you get a 400-line method; keep them apart and each is small and swappable.
💡 That split is the single responsibility principle in action: one class, one reason to change. If the building adds zones you edit the dispatcher; if the scheduling rule changes you edit the movement strategy; the car itself is untouched either way. Learn more →
Functional Requirements
- A building has N floors and M elevator cars.
- Two kinds of request:
- External (hall call): pressed on a floor, has a direction (up/down).
- Internal (car call): pressed inside a car, has a target floor.
- A dispatcher assigns each external request to the most suitable car.
- Each car serves its committed stops in an efficient order (SCAN / LOOK — keep going one way, then reverse).
💡 Both names come from disk-head scheduling. SCAN rides all the way to the end of the building before reversing; LOOK is the polite version that reverses as soon as nothing is left ahead of it in the current direction. - A car tracks its direction: UP, DOWN, or IDLE.
💡 Direction here is the car’s state, and state decides behaviour: a car in UP only looks at stops above it, so the samestep()call does different things depending on which state the car is in. - Doors open on arrival at a committed floor.
Non-Functional Requirements
- No starvation — a request is eventually served, not indefinitely skipped.
💡 Starvation = a request that is legal to serve but keeps losing to newer ones, so it waits forever. A greedy “always go to the nearest call” rule starves the top floor of a busy building. - Extensibility — new dispatch policy or movement strategy = one new class.
- Concurrency-ready — requests can arrive while cars are moving.
💡 Requests landing while cars move is a thread-safety problem: a button press mutates a car’s stop-set at the same moment the movement strategy is reading it, so in a real deployment that shared set needs a lock.
Core Entities
| Entity | Description |
|---|---|
Direction |
Enum: UP, DOWN, IDLE |
Request |
A floor to serve, with an optional direction (hall calls) |
ElevatorCar |
One physical car: current floor, direction, and its set of pending stops |
MovementStrategy |
Given a car’s stops, decide the next floor (SCAN/LOOK today) |
DispatchStrategy |
Given all cars + a request, pick the car to serve it |
ElevatorSystem |
Facade: holds cars, routes requests, ticks the simulation |
💡 A facade is one simple class in front of a complicated set of collaborators. Callers press a button through ElevatorSystem and never have to know that a dispatcher picked a car and a movement strategy picked a floor.
Two strategies, two decisions
The design hinges on splitting the two independent choices:
- Which car? →
DispatchStrategy. Nearest-car is the classic; you could do least-busy, or zone-based. - Next floor for a committed car? →
MovementStrategy. LOOK (a smarter SCAN) keeps moving in the current direction until no more stops lie ahead, then reverses — minimising direction changes.
💡 Strategy pattern = encapsulate each algorithm behind a common interface so the context can swap it at runtime. Two orthogonal decisions → two strategy interfaces, composed independently.
💡 Orthogonal = the two choices do not constrain each other. You can run nearest-car dispatch with LOOK, or zone dispatch with SCAN, in any of the four combinations, because neither interface mentions the other. Learn more →
classDiagram
class Direction {
<<enumeration>>
UP
DOWN
IDLE
}
class ElevatorCar {
-int id
-int currentFloor
-Direction direction
-TreeSet~Integer~ upStops
-TreeSet~Integer~ downStops
+addStop(int floor)
+step(MovementStrategy)
+distanceTo(int floor) int
}
class MovementStrategy {
<<interface>>
+nextFloor(ElevatorCar car) Integer
}
class LookStrategy {
+nextFloor(ElevatorCar) Integer
}
class DispatchStrategy {
<<interface>>
+selectCar(List~ElevatorCar~ cars, Request req) ElevatorCar
}
class NearestCarDispatch {
+selectCar(List, Request) ElevatorCar
}
class ElevatorSystem {
-List~ElevatorCar~ cars
-DispatchStrategy dispatch
-MovementStrategy movement
+requestHallCall(int floor, Direction dir)
+requestCarCall(int carId, int floor)
+step()
}
MovementStrategy <|.. LookStrategy
DispatchStrategy <|.. NearestCarDispatch
ElevatorSystem --> ElevatorCar
ElevatorSystem --> DispatchStrategy
ElevatorSystem --> MovementStrategy
ElevatorCar ..> MovementStrategy
💡 Reading the arrows: a dashed arrow with a hollow head (<|..) means “implements this interface”, a plain arrow (-->) means “holds a reference to”, and a dotted arrow (..>) means “merely uses” — the car is handed a MovementStrategy per call rather than storing one. Learn more →
Key classes:
ElevatorSystem— the entry point. Holds the cars and both strategies, routes a hall call through the dispatcher, sends a car call straight to its car, and drives time forward withstep().ElevatorCar— one physical car. Owns its current floor, its direction, and its two sorted stop-sets; it moves itself one floor perstep()but asks a strategy where to head.MovementStrategy— the “which floor next for this car” interface, withLookStrategyas the supplied answer. A single method,nextFloor(car).LookStrategy— keeps the car going in its current direction while stops remain ahead, then reverses. This is the class that readsceiling()/floor()off the stop-sets.DispatchStrategy— the “which car takes this request” interface, withNearestCarDispatchas the supplied answer. Completely separate from movement.Request— a floor plus an optional direction. Direction is present for hall calls (you pressed up or down in the lobby) and absent for car calls (you pressed 7 inside the car).Direction— UP, DOWN, IDLE. Small enum, but it is the state that decides which stop-set a car is currently serving.
Design Patterns
| Pattern | Where | Why |
|---|---|---|
| Strategy | DispatchStrategy + MovementStrategy |
Two orthogonal, swappable decisions. Zone dispatch or SCAN vs LOOK = one class each. |
| State | Direction drives which stop-set a car serves |
UP serves the up-set ascending, DOWN serves the down-set descending, IDLE picks either. |
| Facade | ElevatorSystem |
One entry point (requestHallCall, step) hides car/dispatch/movement wiring. |
| Command (extension) | Request objects queued and replayed |
Enables logging, replay, and prioritisation. |
💡 State pattern = let the object’s current state decide what an operation does. Direction is the lightweight version of it: there is no UpState class, but UP, DOWN and IDLE still each select a different set of legal next moves.
Discussion
Newest first