Design an Elevator Dispatcher with Cost-Based Pickup Assignment

Quick Overview

Design an elevator dispatcher that assigns each floor pickup to the elevator with the lowest cost, using an on-the-way rule, a reversal penalty and a smallest-id tie break, then simulates movement one floor per step. It tests class design, careful state updates and tracing a simulation by hand.

Design an Elevator Dispatcher with Cost-Based Pickup Assignment

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an elevator dispatcher that manages a fleet of elevators in a building with a fixed number of floors. The dispatcher receives pickup requests from floors and assigns each request to the most suitable elevator. Each elevator has a current floor, a direction (`UP`, `DOWN` or `IDLE`) and a destination queue. When a request `(floor, direction)` arrives, the dispatcher picks the elevator with the lowest assignment cost and adds the floor to its route. The cost to assign a request `(reqFloor, reqDir)` to an elevator currently at `curFloor` with direction `curDir` is: - If the elevator is `IDLE`: `abs(curFloor - reqFloor)`. - If the elevator is moving in the same direction as the request and the request floor is on the way (`curDir == UP` and `reqFloor >= curFloor`, or `curDir == DOWN` and `reqFloor <= curFloor`): `abs(curFloor - reqFloor)`. - Otherwise the elevator must first finish its current direction before reversing, so the cost is a penalty of `2 * numFloors` plus `abs(curFloor - reqFloor)`. If several elevators tie on cost, the elevator with the smallest id wins. Each `step()` advances every elevator one floor toward the next floor in its queue, removing that floor when it is reached and updating the direction. Implement the following: - `ElevatorDispatcher(numElevators: int, numFloors: int)` creates elevators with ids `0` to `numElevators - 1`. Every elevator starts at floor 0, `IDLE`, with an empty queue. - `requestPickup(floor: int, direction: str) -> int` assigns the request to the elevator with the lowest cost (smallest id breaks ties), adds the floor to that elevator's route and returns the chosen elevator id. - `step()` advances the simulation by one time unit. - `getElevatorFloor(elevatorId: int) -> int` returns the current floor of the elevator with the given id. - `getElevatorDirection(elevatorId: int) -> str` returns the current direction of the elevator: `"UP"`, `"DOWN"` or `"IDLE"`. The route and movement rules in detail: - The cost uses each elevator's direction as stored at the moment of the call. - The route is first in, first out. A new floor goes to the back of the chosen elevator's queue. If that floor is already in the queue, nothing is added, and the chosen id is still returned. - When an `IDLE` elevator receives a floor, its direction becomes `UP` or `DOWN` toward that floor right away. It stays `IDLE` if the floor is its current floor. - In `step()`, an elevator with an empty queue becomes `IDLE`. Otherwise it moves one floor toward the floor at the front of its queue and takes that direction; it does not move if it is already there. If it is now at the front floor, that floor is removed and the direction points toward the new front floor, or becomes `IDLE` if the queue is empty. Example 1, two elevators and ten floors: ```text ElevatorDispatcher(2, 10) requestPickup(5, "UP") -> 0 requestPickup(3, "DOWN") -> 1 step() step() step() getElevatorFloor(0) -> 3 getElevatorDirection(0) -> "UP" getElevatorDirection(1) -> "IDLE" requestPickup(1, "UP") -> 1 requestPickup(8, "UP") -> 0 step() step() getElevatorFloor(0) -> 5 getElevatorDirection(0) -> "UP" getElevatorFloor(1) -> 1 getElevatorDirection(1) -> "IDLE" ``` Both elevators are idle at floor 0 for the first request, so they tie at cost 5 and elevator 0 wins. Elevator 0 is now `UP`, so the `DOWN` request at floor 3 costs it `2 * 10 + 3 = 23`, while idle elevator 1 costs 3. After three steps elevator 1 has reached floor 3 and is idle again. The `UP` request at floor 1 is below elevator 0, which is moving up, so elevator 0 costs 22 and elevator 1 costs 2. The `UP` request at floor 8 is on elevator 0's way (cost 5), while elevator 1 is now moving down (cost 25). Two steps later elevator 0 reaches floor 5, removes it and keeps going up toward 8, and elevator 1 reaches floor 1 and becomes idle. Example 2, one elevator (`step() x 6` stands for six calls in a row): ```text ElevatorDispatcher(1, 10) requestPickup(6, "DOWN") -> 0 requestPickup(2, "UP") -> 0 requestPickup(6, "UP") -> 0 step() x 6 getElevatorFloor(0) -> 6 getElevatorDirection(0) -> "DOWN" step() x 4 getElevatorFloor(0) -> 2 getElevatorDirection(0) -> "IDLE" requestPickup(2, "DOWN") -> 0 getElevatorDirection(0) -> "IDLE" step() getElevatorFloor(0) -> 2 ``` The queue is `[6, 2]` after the first two requests, and the repeated request for floor 6 adds nothing. The elevator passes floor 2 on the way up without stopping, because 6 is at the front of the queue. At floor 6 it turns `DOWN` toward 2 and becomes `IDLE` when it arrives. The last request is for the floor where the idle elevator already stands, so its direction stays `IDLE` and the next step removes that floor without moving. ```hint Read state at call time Several requests can arrive before the next step. Trace what each elevator's direction is at the moment of each call, including an elevator that was idle a moment ago. ``` ```hint Who owns what List each piece of state and decide which object should own it. Then ask which operations need to look at more than one elevator. ``` ### Constraints & Assumptions - `1 <= numElevators <= 16` - `2 <= numFloors <= 200` - `0 <= floor < numFloors` - `direction` is either `"UP"` or `"DOWN"`. - At most `10^4` calls in total to `requestPickup`, `step`, `getElevatorFloor` and `getElevatorDirection`. - `elevatorId` is always a valid id. ### Clarifying Questions to Ask - Do riders also press destination buttons inside the car, and should those floors join the same queue? - Once a pickup is assigned, may it move to another elevator that later becomes a better choice? - Can an elevator be taken out of service, and what happens to the floors in its queue? - Is there a car capacity limit the dispatcher should respect? ### What a Strong Answer Covers - A class split between the dispatcher and the elevator, and the state each one owns. - The cost rule as stated, including the on-the-way test, the reversal penalty and the smallest-id tie break. - How `step` moves an elevator, removes a reached floor and updates the direction, including an idle elevator whose target is its own floor. - Cost per call and edge cases such as duplicate requests and several requests before one step. ### Follow-up Questions - A request on the way gets a low cost but still waits at the back of a first-in, first-out queue. How would you order stops so each elevator serves floors in travel order? - How would you add destination requests from inside the car? - How would you change the cost to account for stops already queued, the real reversal distance or the load of each car? - How would you keep the dispatcher correct when requests arrive from many threads while `step` runs?

Overview: Design an elevator dispatcher that assigns each floor pickup to the elevator with the lowest cost, using an on-the-way rule, a reversal penalty and a smallest-id tie break, then simulates movement one floor per step. It tests class design, careful state updates and tracing a simulation by hand.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an elevator dispatcher that manages a fleet of elevators in a building with a fixed number of floors. The dispatcher receives pickup requests from floors and assigns each request to the most suitable elevator.

Each elevator has a current floor, a direction (UP, DOWN or IDLE) and a destination queue. When a request (floor, direction) arrives, the dispatcher picks the elevator with the lowest assignment cost and adds the floor to its route.

The cost to assign a request (reqFloor, reqDir) to an elevator currently at curFloor with direction curDir is:

  • If the elevator is IDLE : abs(curFloor - reqFloor) .
  • If the elevator is moving in the same direction as the request and the request floor is on the way ( curDir == UP and reqFloor >= curFloor , or curDir == DOWN and reqFloor <= curFloor ): abs(curFloor - reqFloor) .
  • Otherwise the elevator must first finish its current direction before reversing, so the cost is a penalty of 2 * numFloors plus abs(curFloor - reqFloor) .

If several elevators tie on cost, the elevator with the smallest id wins. Each step() advances every elevator one floor toward the next floor in its queue, removing that floor when it is reached and updating the direction.

Implement the following:

  • ElevatorDispatcher(numElevators: int, numFloors: int) creates elevators with ids 0 to numElevators - 1 . Every elevator starts at floor 0, IDLE , with an empty queue.
  • requestPickup(floor: int, direction: str) -> int assigns the request to the elevator with the lowest cost (smallest id breaks ties), adds the floor to that elevator's route and returns the chosen elevator id.
  • step() advances the simulation by one time unit.
  • getElevatorFloor(elevatorId: int) -> int returns the current floor of the elevator with the given id.
  • getElevatorDirection(elevatorId: int) -> str returns the current direction of the elevator: "UP" , "DOWN" or "IDLE" .

The route and movement rules in detail:

  • The cost uses each elevator's direction as stored at the moment of the call.
  • The route is first in, first out. A new floor goes to the back of the chosen elevator's queue. If that floor is already in the queue, nothing is added, and the chosen id is still returned.
  • When an IDLE elevator receives a floor, its direction becomes UP or DOWN toward that floor right away. It stays IDLE if the floor is its current floor.
  • In step() , an elevator with an empty queue becomes IDLE . Otherwise it moves one floor toward the floor at the front of its queue and takes that direction; it does not move if it is already there. If it is now at the front floor, that floor is removed and the direction points toward the new front floor, or becomes IDLE if the queue is empty.

Example 1, two elevators and ten floors:

ElevatorDispatcher(2, 10)
requestPickup(5, "UP")     -> 0
requestPickup(3, "DOWN")   -> 1
step()
step()
step()
getElevatorFloor(0)        -> 3
getElevatorDirection(0)    -> "UP"
getElevatorDirection(1)    -> "IDLE"
requestPickup(1, "UP")     -> 1
requestPickup(8, "UP")     -> 0
step()
step()
getElevatorFloor(0)        -> 5
getElevatorDirection(0)    -> "UP"
getElevatorFloor(1)        -> 1
getElevatorDirection(1)    -> "IDLE"

Both elevators are idle at floor 0 for the first request, so they tie at cost 5 and elevator 0 wins. Elevator 0 is now UP, so the DOWN request at floor 3 costs it 2 * 10 + 3 = 23, while idle elevator 1 costs 3. After three steps elevator 1 has reached floor 3 and is idle again. The UP request at floor 1 is below elevator 0, which is moving up, so elevator 0 costs 22 and elevator 1 costs 2. The UP request at floor 8 is on elevator 0's way (cost 5), while elevator 1 is now moving down (cost 25). Two steps later elevator 0 reaches floor 5, removes it and keeps going up toward 8, and elevator 1 reaches floor 1 and becomes idle.

Example 2, one elevator (step() x 6 stands for six calls in a row):

ElevatorDispatcher(1, 10)
requestPickup(6, "DOWN")   -> 0
requestPickup(2, "UP")     -> 0
requestPickup(6, "UP")     -> 0
step() x 6
getElevatorFloor(0)        -> 6
getElevatorDirection(0)    -> "DOWN"
step() x 4
getElevatorFloor(0)        -> 2
getElevatorDirection(0)    -> "IDLE"
requestPickup(2, "DOWN")   -> 0
getElevatorDirection(0)    -> "IDLE"
step()
getElevatorFloor(0)        -> 2

The queue is [6, 2] after the first two requests, and the repeated request for floor 6 adds nothing. The elevator passes floor 2 on the way up without stopping, because 6 is at the front of the queue. At floor 6 it turns DOWN toward 2 and becomes IDLE when it arrives. The last request is for the floor where the idle elevator already stands, so its direction stays IDLE and the next step removes that floor without moving.

Constraints & Assumptions

  • 1 <= numElevators <= 16
  • 2 <= numFloors <= 200
  • 0 <= floor < numFloors
  • direction is either "UP" or "DOWN" .
  • At most 10^4 calls in total to requestPickup , step , getElevatorFloor and getElevatorDirection .
  • elevatorId is always a valid id.

Clarifying Questions to Ask Guidance

  • Do riders also press destination buttons inside the car, and should those floors join the same queue?
  • Once a pickup is assigned, may it move to another elevator that later becomes a better choice?
  • Can an elevator be taken out of service, and what happens to the floors in its queue?
  • Is there a car capacity limit the dispatcher should respect?

What a Strong Answer Covers Guidance

  • A class split between the dispatcher and the elevator, and the state each one owns.
  • The cost rule as stated, including the on-the-way test, the reversal penalty and the smallest-id tie break.
  • How step moves an elevator, removes a reached floor and updates the direction, including an idle elevator whose target is its own floor.
  • Cost per call and edge cases such as duplicate requests and several requests before one step.

Follow-up Questions Guidance

  • A request on the way gets a low cost but still waits at the back of a first-in, first-out queue. How would you order stops so each elevator serves floors in travel order?
  • How would you add destination requests from inside the car?
  • How would you change the cost to account for stops already queued, the real reversal distance or the load of each car?
  • How would you keep the dispatcher correct when requests arrive from many threads while step runs?