Low-Level Design Tutorial 0/42 lessons ~6 min read Lesson 34

    Elevator System

    Elevator LLD challenges scheduling, state management, and request queues — internal requests (inside cabin) vs external (floor up/down).

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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.
    text
    classDiagram
    class ElevatorController {
    +requestFloor(int)
    }
    class Elevator
    class RequestQueue
    ElevatorController --> Elevator
    ElevatorController --> RequestQueue

    Step-by-step explanation

    1. User presses external button; controller picks elevator.
    2. Elevator adds request to internal queue.
    3. Elevator moves floor by floor servicing queue.
    4. Stop, open door, close door, continue or idle.
    5. Direction reversal when no requests ahead.
    6. Controller balances load across cars.

    Informative example

    Elevator with request queue and simple SCAN-style servicing:

    java
    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?
    Elevator, ElevatorController, Request, DispatchStrategy.
    Q2BeginnerSCAN algorithm?
    Elevator continues current direction servicing all requests until empty, then reverses.
    Q3IntermediateNearest car dispatch?
    Pick elevator minimizing distance to requested floor.
    Q4IntermediatePrevent starvation?
    Limit consecutive same-direction service or mix SCAN with priority floors.
    Q5AdvancedDesign freight vs passenger elevators.
    Separate Elevator subtype or capacity flag; dispatcher filters eligible cars.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.