DSA Tracker

Low-level design

Design an elevator system

Design the control software for a building with several elevators.

Good first question for: SDE-1

Requirements to confirm first

  • Hall buttons on each floor (up and down) and floor buttons inside each car
  • A hall call is assigned to one car, and the car serves its stops in a sensible order
  • Each car has a direction, a current floor and a door state
  • A car does not reverse while it still has stops ahead in its direction
  • Ask: how many cars and floors, is there a capacity limit, is there a priority or fire mode?

Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.

Show the answer key (write yours first)

Classes and what each one owns

ElevatorController
Receives hall calls and picks a car for each
Elevator
One car: its floor, direction, door and its own stops
Request
A floor plus a direction for a hall call, a floor only for a call from inside a car
DispatchStrategy
Interface that chooses the car for a hall call
Door
Open, closed, opening, closing, and the rule for an obstruction
Direction
Enum: UP, DOWN, IDLE

How they connect

  • The controller has many Elevators and one DispatchStrategy
  • Each Elevator owns its stops; the controller never edits them, it calls addStop
  • Buttons inside a car go straight to that Elevator; only hall calls go through the controller

Design patterns that fit, and why

Strategy
The dispatch rule (nearest car, least loaded, zones) changes per building and is the main follow-up.
State
Car movement: idle, moving up, moving down, doors open. Each state decides what one step does.
Observer
Floor displays and hall lamps listen to car events instead of being called by the car.

Code sketch in Java

The core types and one real method. It is a sketch to talk through, not a full program.

interface DispatchStrategy { Elevator choose(List<Elevator> cars, Request call); }

class Elevator {
  private int floor;
  private Direction direction = Direction.IDLE;
  private final TreeSet<Integer> upStops = new TreeSet<>();
  private final TreeSet<Integer> downStops = new TreeSet<>();
  private final Door door = new Door();

  void addStop(int target) {
    if (target == floor) { door.open(); return; }
    (target > floor ? upStops : downStops).add(target);
  }

  // one step: keep going while stops remain ahead, then turn around or go idle
  void step() { /* serve upStops in rising order, downStops in falling order */ }
}

Edge cases to raise yourself

  • A call for the floor the car is already on
  • A car going up receives a call from below: it waits until the car turns
  • Two cars answering the same hall call
  • A car goes out of service with stops still assigned to it

Where answers usually go wrong

  • First come, first served. The car then crosses the building for every request. Say 'keep direction until no stops remain ahead' early.
  • Putting the choice of car inside Elevator. One car cannot see the others; that decision belongs to the controller's strategy.
  • Modelling floors as objects with behaviour. A floor is a number and two buttons.
  • Skipping the door. Half of the real rules live there: do not move with the door open, reopen on an obstruction.

Follow-up questions

  • Add an express car that serves only some floors
  • Morning rush: most calls start at the ground floor. What changes in dispatch?
  • Fire mode: every car returns to the ground floor