WHILELOOPWARRIOR(1)Design Interview ManualWHILELOOPWARRIOR(1)

~/lld/elevator-system

Elevator System · 9006

A hard low-level design interview question on scheduling, state, simulation, strategy. 8 scenario tests.

SYNOPSIS

interviewer: Design an elevator system.

The controller for a bank of elevators in one building. About 45 minutes.

QUESTIONS TO ASK

Interviews start vague on purpose. Strong candidates ask before they draw; in the studio the interviewer answers these and the answers become your requirements.

  • How many elevators and floors?
  • What kinds of requests are there?
  • Which elevator answers a hall call?
  • In what order does a car serve its stops?
  • How do we model time so it's testable?
  • What does the display need?
  • Door timing, weight limits?
  • Fire mode, emergency stop?

DESCRIPTION

The full statement, as the studio shows it at the implement step.

Design the controller for a bank of elevators in one building. Time moves in ticks so the behaviour is testable.

setup

new ElevatorSystem({ elevators: N, floors: F }): elevators 0..N-1, floors 0..F-1. Every elevator starts at floor 0, idle, with no stops.

requests

  • press(elevator, floor): someone inside elevator `elevator` presses a floor button. Adds a stop to that elevator.
  • request(floor, direction): someone on a floor presses up or down. The system assigns the call to one elevator (adds a stop) and returns its id. Cost of an elevator for the call:
|floor - elevator.floor|            when the elevator is idle, or moving
                                     in `direction` with the floor ahead
                                     of it (or at it)
|floor - elevator.floor| + 2 * F    otherwise (it would have to turn)

The lowest cost wins; ties go to the lowest elevator id.

  • Floors or elevator ids out of range throw an Error containing "out of range".

one tick : step()

Each elevator, in id order:

  • If its current floor is one of its stops, it opens its doors: the stop is removed and it does not move this tick.
  • Otherwise, if it has stops, it moves one floor in its direction:
  • keep going up while there is a stop above, keep going down while there is a stop below (that is SCAN, like a disk head);
  • an idle elevator heads for its nearest stop (a tie goes up);
  • when nothing is left ahead, it turns around.
  • At the end of the tick its direction is "idle" if it has no stops left, otherwise "up" or "down", the way it will go next.

status() returns, for every elevator in id order:

{ id, floor, direction: 'up' | 'down' | 'idle', stops: [ascending floors] }

The direction only changes during step(), not when a request arrives.

think about

  • Separate "where should each elevator go" (scheduling strategy) from "move the car" (state), so a smarter scheduler can replace this one.
  • Peak hours: everyone goes down at 6 pm. What would you change?

GRADING

The studio runs the interview in seven steps (clarify, entities, class diagram, patterns, sequence, implement, review) and a coach grades each one live: the core objects you found, the relations between them (ownership vs. use, the right end of the diamond), responsibilities, whether your patterns really have the shape of the pattern, and a sequence diagram of the main use case. Then you implement it in JavaScript and run its 8 scenario tests in your browser. The review gives a score out of 100, a hire signal and a reference design to compare.

FOLLOW-UPS

What the interviewer asks when you finish early:

  • A weight sensor: a full car should skip hall calls but still serve presses from inside. Where does that rule belong?
  • Express elevators that only serve floors 0 and 20+.

SEE ALSO