Simulate a multi-elevator controller with nearest-car dispatch and discrete time steps

Quick Overview

Build an in-memory elevator bank that parses floor requests, assigns each one to the nearest car with fixed tie-breakers, and moves every car one floor per step until all stops are served. It tests object modeling, per-car state machines and careful, deterministic simulation logic.

Simulate a multi-elevator controller with nearest-car dispatch and discrete time steps

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

A simple in-memory simulation of a bank of elevators serving a range of floors, processing hall requests in discrete time steps. Implement the following methods: - `ElevatorController(n: int, minFloor: int, maxFloor: int) → void`: Construct a controller with `n` cars serving floors `minFloor` to `maxFloor`. Cars are numbered `0` to `n - 1`, and every car starts at `minFloor`, `STOPPED` and `IDLE`. - `processRequests(cmds: string[]) → void`: Parse and enqueue each `"REQUEST F DIR"` command; then repeatedly advance time until no elevator has pending requests. - `getElevatorFloor(eid: int) → int`: Return the current floor of car `eid`. - `getElevatorState(eid: int) → ElevatorState`: Return the current state of car `eid`: `MOVING`, `STOPPED`, or `DOOR_OPEN`. - `getElevatorDirection(eid: int) → Direction`: Return the current travel direction of car `eid`: `UP`, `DOWN`, or `IDLE`. Notes: - `"REQUEST F DIR"` strings use integer floor `F` and direction `DIR` in `{UP, DOWN}`. The direction is recorded with the request but does not affect dispatch. - Dispatch policy is "nearest-car": each request is assigned to the elevator whose current floor is closest to `F`. All requests in a batch are assigned before time starts, so distances use the floors where the cars stood when the batch began. When several cars are equally close, the request goes to the car with fewer pending requests, and then to the lower car ID. - Each car serves its own requests nearest-first. It heads for the pending floor closest to its current floor (if two are equally close, the one requested first) and chooses again after each stop. - Time advances in discrete "step" units hidden inside `processRequests`. In one step a car either moves one floor toward its next stop (`MOVING`) or, if it is already there, serves every request waiting at that floor. Doors open instantly (no real wait), so the car is `STOPPED` again at the end of that step. - Once all pending requests are served, elevators reset direction to `IDLE`. Example 1: ```text Input: operations = ["ElevatorController", "processRequests", "getElevatorFloor", "getElevatorFloor", "getElevatorState", "getElevatorDirection"] arguments = [[2, 1, 10], [["REQUEST 3 UP", "REQUEST 7 DOWN", "REQUEST 5 UP"]], [0], [1], [1], [1]] Output: [null, null, 5, 7, "STOPPED", "IDLE"] ``` Both cars start at floor 1, so each request is equally close to both. Floor 3 goes to car 0 (lower ID), floor 7 goes to car 1 (it has no pending requests, while car 0 has one), and floor 5 goes to car 0 (one pending request each, lower ID). Car 0 travels from 1 to 3 and then to 5, car 1 travels from 1 to 7, and both end `STOPPED` and `IDLE`. Example 2: ```text Input: operations = ["ElevatorController", "processRequests", "getElevatorFloor", "processRequests", "getElevatorFloor", "getElevatorDirection"] arguments = [[1, 0, 15], [["REQUEST 10 UP"]], [0], [["REQUEST 8 DOWN", "REQUEST 13 UP", "REQUEST 5 DOWN"]], [0], [0]] Output: [null, null, 10, null, 13, "IDLE"] ``` The single car waits at floor 10 after the first batch. In the second batch it goes to floor 8 first (2 floors away, against 3 for floor 13 and 5 for floor 5). From floor 8, floor 5 is 3 floors away and floor 13 is 5, so it goes down to 5 and finishes at 13. ```hint Two phases per batch Split a batch into dispatch and simulation. Decide what each car must store so the time loop never needs to look at the other cars. ``` ```hint Ties decide the first example Every request in Example 1 is equally close to both cars. Express the dispatch choice as one comparison key that includes both tie-breakers, and trace the example with it before you write the time loop. ``` ### Constraints & Assumptions - `1 <= n <= 16` - `minFloor < maxFloor`, and `maxFloor - minFloor <= 100` - `1 <= number of requests in a single batch <= 10⁴` - Total calls to `processRequests`, `getElevatorFloor`, `getElevatorState`, `getElevatorDirection` `<= 10⁴` - All commands and parameters are well-formed strings or valid integers. ### Clarifying Questions to Ask - Can new requests arrive while the cars are moving, or only in whole batches between calls? - Does opening the doors take a time step, and should `DOOR_OPEN` ever be visible from the getters? - What should happen to a command that is malformed or names a floor outside `minFloor` to `maxFloor`? - Should the request direction influence which car is chosen, for example by preferring a car already travelling that way? ### What a Strong Answer Covers - A class split that keeps each car's floor, state, direction and pending stops inside the car, and leaves parsing, dispatch and the time loop to the controller. - The dispatch rule implemented exactly, including both tie-breakers, using the cars' floors at the start of the batch. - A per-car step function that moves one floor at a time, picks the nearest pending floor after each stop, and leaves the car `STOPPED` and `IDLE` when its work runs out. - A clear termination condition for the time loop, and a correct trace of both examples. - The cost of dispatch and of simulating a full batch in terms of requests, cars and floors. ### Follow-up Questions - Nearest-first makes the car in Example 2 zig-zag and can starve a distant floor. How would you change each car to keep moving in one direction while it has stops ahead? - How would you use the request direction in dispatch, so a car heading up is not sent to collect someone going down? - How would you add cabin requests (a destination chosen inside a car) and a capacity limit per car? - If requests arrived from many threads while the cars ran in real time, what would you lock, and how would you keep dispatch consistent?

Overview: Build an in-memory elevator bank that parses floor requests, assigns each one to the nearest car with fixed tie-breakers, and moves every car one floor per step until all stops are served. It tests object modeling, per-car state machines and careful, deterministic simulation logic.

Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

A simple in-memory simulation of a bank of elevators serving a range of floors, processing hall requests in discrete time steps.

Implement the following methods:

  • ElevatorController(n: int, minFloor: int, maxFloor: int) → void : Construct a controller with n cars serving floors minFloor to maxFloor . Cars are numbered 0 to n - 1 , and every car starts at minFloor , STOPPED and IDLE .
  • processRequests(cmds: string[]) → void : Parse and enqueue each "REQUEST F DIR" command; then repeatedly advance time until no elevator has pending requests.
  • getElevatorFloor(eid: int) → int : Return the current floor of car eid .
  • getElevatorState(eid: int) → ElevatorState : Return the current state of car eid : MOVING , STOPPED , or DOOR_OPEN .
  • getElevatorDirection(eid: int) → Direction : Return the current travel direction of car eid : UP , DOWN , or IDLE .

Notes:

  • "REQUEST F DIR" strings use integer floor F and direction DIR in {UP, DOWN} . The direction is recorded with the request but does not affect dispatch.
  • Dispatch policy is "nearest-car": each request is assigned to the elevator whose current floor is closest to F . All requests in a batch are assigned before time starts, so distances use the floors where the cars stood when the batch began. When several cars are equally close, the request goes to the car with fewer pending requests, and then to the lower car ID.
  • Each car serves its own requests nearest-first. It heads for the pending floor closest to its current floor (if two are equally close, the one requested first) and chooses again after each stop.
  • Time advances in discrete "step" units hidden inside processRequests . In one step a car either moves one floor toward its next stop ( MOVING ) or, if it is already there, serves every request waiting at that floor. Doors open instantly (no real wait), so the car is STOPPED again at the end of that step.
  • Once all pending requests are served, elevators reset direction to IDLE .

Example 1:

Input:
operations = ["ElevatorController", "processRequests", "getElevatorFloor", "getElevatorFloor", "getElevatorState", "getElevatorDirection"]
arguments  = [[2, 1, 10], [["REQUEST 3 UP", "REQUEST 7 DOWN", "REQUEST 5 UP"]], [0], [1], [1], [1]]

Output:
[null, null, 5, 7, "STOPPED", "IDLE"]

Both cars start at floor 1, so each request is equally close to both. Floor 3 goes to car 0 (lower ID), floor 7 goes to car 1 (it has no pending requests, while car 0 has one), and floor 5 goes to car 0 (one pending request each, lower ID). Car 0 travels from 1 to 3 and then to 5, car 1 travels from 1 to 7, and both end STOPPED and IDLE.

Example 2:

Input:
operations = ["ElevatorController", "processRequests", "getElevatorFloor", "processRequests", "getElevatorFloor", "getElevatorDirection"]
arguments  = [[1, 0, 15], [["REQUEST 10 UP"]], [0], [["REQUEST 8 DOWN", "REQUEST 13 UP", "REQUEST 5 DOWN"]], [0], [0]]

Output:
[null, null, 10, null, 13, "IDLE"]

The single car waits at floor 10 after the first batch. In the second batch it goes to floor 8 first (2 floors away, against 3 for floor 13 and 5 for floor 5). From floor 8, floor 5 is 3 floors away and floor 13 is 5, so it goes down to 5 and finishes at 13.

Constraints & Assumptions

  • 1 <= n <= 16
  • minFloor < maxFloor , and maxFloor - minFloor <= 100
  • 1 <= number of requests in a single batch <= 10⁴
  • Total calls to processRequests , getElevatorFloor , getElevatorState , getElevatorDirection <= 10⁴
  • All commands and parameters are well-formed strings or valid integers.

Clarifying Questions to Ask Guidance

  • Can new requests arrive while the cars are moving, or only in whole batches between calls?
  • Does opening the doors take a time step, and should DOOR_OPEN ever be visible from the getters?
  • What should happen to a command that is malformed or names a floor outside minFloor to maxFloor ?
  • Should the request direction influence which car is chosen, for example by preferring a car already travelling that way?

What a Strong Answer Covers Guidance

  • A class split that keeps each car's floor, state, direction and pending stops inside the car, and leaves parsing, dispatch and the time loop to the controller.
  • The dispatch rule implemented exactly, including both tie-breakers, using the cars' floors at the start of the batch.
  • A per-car step function that moves one floor at a time, picks the nearest pending floor after each stop, and leaves the car STOPPED and IDLE when its work runs out.
  • A clear termination condition for the time loop, and a correct trace of both examples.
  • The cost of dispatch and of simulating a full batch in terms of requests, cars and floors.

Follow-up Questions Guidance

  • Nearest-first makes the car in Example 2 zig-zag and can starve a distant floor. How would you change each car to keep moving in one direction while it has stops ahead?
  • How would you use the request direction in dispatch, so a car heading up is not sent to collect someone going down?
  • How would you add cabin requests (a destination chosen inside a car) and a capacity limit per car?
  • If requests arrived from many threads while the cars ran in real time, what would you lock, and how would you keep dispatch consistent?