Elevator System
Elevator LLD challenges scheduling, state management, and request queues — internal requests (inside cabin) vs external (floor up/down).
Introduction
Elevator LLD challenges scheduling, state management, and request queues — internal requests (inside cabin) vs external (floor up/down). Interviewers expect Elevator, ElevatorController, Request, and a dispatch strategy.
Model elevator state: Idle, Moving, DoorOpen. Controller assigns requests using nearest-car or SCAN algorithm. Discuss multiple elevators in one shaft bank.
Start single elevator; extend to fleet dispatch.
Understanding the topic
Key concepts
- Elevator: current floor, direction, door state, request queue.
- ExternalRequest: floor + direction; InternalRequest: destination floor.
- Controller selects elevator for external call.
- Scheduling: FCFS, nearest, SCAN (elevator algorithm).
- Safety: don't move with door open.
- Multiple elevators share RequestDispatcher.
classDiagramclass ElevatorController {+requestFloor(int)}class Elevatorclass RequestQueueElevatorController --> ElevatorElevatorController --> RequestQueue
Step-by-step explanation
- User presses external button; controller picks elevator.
- Elevator adds request to internal queue.
- Elevator moves floor by floor servicing queue.
- Stop, open door, close door, continue or idle.
- Direction reversal when no requests ahead.
- Controller balances load across cars.
Informative example
Elevator with request queue and simple SCAN-style servicing:
public enum Direction { UP, DOWN, IDLE }public final class Elevator {private final int id;private int currentFloor;private Direction direction = Direction.IDLE;private final TreeSet<Integer> targets = new TreeSet<>();public Elevator(int id, int startFloor) {this.id = id;this.currentFloor = startFloor;}public int id() { return id; }public int currentFloor() { return currentFloor; }public void addRequest(int floor) {targets.add(floor);updateDirection();}public void step() {if (targets.isEmpty()) {direction = Direction.IDLE;return;}if (direction == Direction.IDLE) updateDirection();if (direction == Direction.UP) currentFloor++;else if (direction == Direction.DOWN) currentFloor--;if (targets.contains(currentFloor)) {targets.remove(currentFloor);}if (targets.isEmpty()) direction = Direction.IDLE;else updateDirection();}private void updateDirection() {if (targets.isEmpty()) direction = Direction.IDLE;else if (targets.last() > currentFloor) direction = Direction.UP;else if (targets.first() < currentFloor) direction = Direction.DOWN;}}public final class ElevatorController {private final List<Elevator> cars;private final DispatchStrategy strategy;public ElevatorController(List<Elevator> cars, DispatchStrategy strategy) {this.cars = cars;this.strategy = strategy;}public void requestPickup(int floor, Direction dir) {int carId = strategy.selectElevator(cars, floor);cars.stream().filter(c -> c.id() == carId).findFirst().orElseThrow().addRequest(floor);}public void tick() { cars.forEach(Elevator::step); }}
Production adds Door state and capacity; SCAN strategy module for interview extension.
Real-world use
Real-world applications
- State machine + strategy combo question.
- Scheduling algorithm discussion.
- Simulation tick loop design.
Best practices
- Separate Request types internal vs external.
- Strategy pattern for dispatch algorithms.
- TreeSet or priority queue for floor ordering.
- State enum guards illegal transitions.
- Controller does not move elevator directly — elevator owns motion.
Common mistakes
- No direction handling — elevator oscillates randomly.
- Controller implementing all movement logic.
- Ignoring door open safety state.
- Single global queue without per-elevator queues.
Advanced interview questions
Q1BeginnerCore elevator classes?
Q2BeginnerSCAN algorithm?
Q3IntermediateNearest car dispatch?
Q4IntermediatePrevent starvation?
Q5AdvancedDesign freight vs passenger elevators.
Summary
Elevator manages floor queue and direction. Controller dispatches external requests. Strategy pattern for scheduling algorithms. State machine for door and motion safety. Extend from one car to elevator bank.