THN Interview Prep

Design Elevator System (LLD)

1. Requirements

  • Functional

    • Building has floors and one or more elevator cars; passengers request up/down from hall or choose destination inside cab.
    • Dispatch assigns cars to pick up and drop off passengers according to a scheduling policy.
    • Cars move floor-to-floor with doors that open when idle at a serviced stop.
    • Support emergency stop and maintenance mode (optional in scope).
  • Non-Functional

    • Dispatch decisions must be explainable and safe (no skipping mandatory stops incorrectly).
    • Extensible scheduling (SCAN, LOOK, FCFS) without rewriting car physics.
    • Reasonable worst-case response under concurrent hall calls.
  • Assumptions / Out of Scope

    • Single controller process; no distributed consensus for one building.
    • Weight sensor and fire-safety interlocks described as interfaces only.

2. Core Entities

EntityResponsibilityKey Attributes
BuildingOwns floors and elevator shaftsidentifier, floors, cars
ElevatorCarVertical transport unitidentifier, currentFloor, direction, doorState, load
FloorVertical position indexindex, hallButtons
DispatchControllerAssigns cars to requestspolicy, requestQueues
ElevatorRequestIntent to travelsourceFloor, destinationFloor, directionHint
SchedulingPolicyOrders stops for a carname, nextStop logic

3. Class Diagram

Loading diagram…

4. State / Sequence Diagram (where relevant)

Loading diagram…

5. Design Patterns Applied

6. Implementation

Go

package elevator

type Direction int

type ElevatorCar struct {
    Identifier   string
    CurrentFloor int
    Moving       Direction
    DoorOpen     bool
    InternalStops map[int]struct{}
}

type DispatchController struct {
    Cars   []*ElevatorCar
    Policy SchedulingPolicy
}

func (controller *DispatchController) SubmitHallCall(floor int, direction Direction) { /* ... */ }
func (controller *DispatchController) Tick() { /* advances simulation / scheduler */ }

JavaScript

class ElevatorCar {
  constructor({ identifier, startingFloor }) {
    this.identifier = identifier;
    this.currentFloor = startingFloor;
    this.direction = 'idle';
    this.doorOpen = false;
    this.internalStops = new Set();
  }
}

class DispatchController {
  constructor({ cars, schedulingPolicy }) {
    this.cars = cars;
    this.schedulingPolicy = schedulingPolicy;
  }
  submitHallCall({ floorIndex, direction }) { /* ... */ }
  tick() { /* ... */ }
}

7. Concurrency / Thread Safety

  • Collisions: Concurrent hall calls mutating one car's stop set; concurrent tick vs external submit.
  • Granularity: One mutex per car for stop sets and door state; controller serializes global dispatch step or uses channel per car in Go.
  • Go: sync.Mutex on each ElevatorCar; controller loop receives events on channel.
  • JavaScript: Single event loop; use explicit queue for simulation ticks if workers added later.

8. Extensibility & Followups

  • Destination dispatch: passengers enter target floor at lobby before boarding.
  • Energy optimization: idle parking floor policy as another SchedulingPolicy.
  • Multi-shaft: partition hall calls by zone then merge results.
  • Edge cases: overload, door obstruction retries, power loss mid-move.

Last updated on

Spotted something unclear or wrong on this page?

On this page