DesignTheSystem/dev
HARD Low-Level Design (LLD) 13 automated tests

Design Elevator System (Low-Level Design)

Several elevators answering hall calls, moving one floor per step and serving stops in their direction of travel. Focus: A dispatch strategy and a step-by-step state machine.

DifficultyHARD
Design focusA dispatch strategy and a step-by-step state machine
Automated grading3 public + 10 hidden tests (13 total)
Phase 2 follow-upOut of service
LanguageTypeScript (OOP / Machine Coding)

Problem statement

Floors are numbered 0 to floors − 1. Every elevator starts at floor 0, IDLE, with no stops. Time moves only when step() is called.

request(floor, direction) is a hall call. It goes to the elevator with the lowest cost, ties to the lowest id. Cost: an IDLE elevator costs its distance to the floor; an elevator already moving in the call's direction with the floor still ahead (or at its floor) costs the distance; anything else costs the distance + 2 × floors. The floor becomes one of that elevator's stops.

On each step(), every elevator, in id order: if its current floor is one of its stops, it opens its doors there (the stop is removed, it does not move; with no stops left it becomes IDLE). Otherwise, with no stops it is IDLE. Otherwise it moves one floor: it keeps its direction while there is a stop ahead in that direction; if not, it heads for the nearest stop (ties: up).

Functional requirements

  • new ElevatorSystem(elevators, floors); request(floor, direction) returns the elevator id.
  • pressFloor(elevatorId, floor) adds a stop from inside the car.
  • status() returns { id, floor, direction, stops } for each elevator, stops sorted low to high. direction is UP, DOWN or IDLE.
  • Floors outside the building, unknown elevators and directions other than UP and DOWN throw.

Required API & starter interface

Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:

type Direction = "UP" | "DOWN";
type Motion = Direction | "IDLE";

interface ElevatorStatus {
  id: number;
  floor: number;
  direction: Motion;
  stops: number[];
}

class ElevatorSystem {
  constructor(elevators: number, floors: number) {}

  /** A hall call; returns the id of the elevator that will come */
  request(floor: number, direction: Direction): number {
    throw new Error("Not implemented");
  }

  pressFloor(elevatorId: number, floor: number): void {
    throw new Error("Not implemented");
  }

  step(): void {
    throw new Error("Not implemented");
  }

  status(): ElevatorStatus[] {
    throw new Error("Not implemented");
  }
}

Public test cases

Your code runs against 3 public tests plus 10 hidden edge-case tests in a sandbox:

An elevator goes to a call and opens (core)

var s = new ElevatorSystem(1, 10);
assertEqual(s.request(3, "UP"), 0);
s.step(); s.step(); s.step();
var e = s.status()[0];
assertEqual([e.floor, e.direction, e.stops], [3, "UP", [3]]);
s.step();
e = s.status()[0];
assertEqual([e.floor, e.direction, e.stops], [3, "IDLE", []]);

The nearest idle elevator answers (core)

var s = new ElevatorSystem(2, 10);
s.pressFloor(1, 5);
for (var i = 0; i < 6; i++) s.step();
assertEqual(s.status()[1].floor, 5);
assertEqual(s.request(6, "DOWN"), 1);

Floors outside the building (edge)

var s = new ElevatorSystem(1, 10);
assertThrows(function () { s.request(10, "UP"); });
assertThrows(function () { s.pressFloor(0, -1); });

Design & structure checks

Beyond passing tests, the grader inspects your class structure for clean object-oriented design:

  • Choosing an elevator is a separate strategy: Dispatch rules change (nearest car, least busy, zones). A strategy object lets you swap them without touching how elevators move.
  • Each elevator is an object that moves itself: Keeping floor, direction and stops inside an Elevator class keeps the stepping rules in one place.

Phase 2: Change request

In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 13 tests, you unlock the change request: Out of service.

Relevant low-level design lessons

More low-level design problems