Elevator System Design in Java: LOOK and a Dispatcher
Code an elevator system in Java for a machine-coding round: a TreeSet of stops, direction as state, LOOK scheduling and a dispatcher for two cars.
- Course: Low-Level Design study plan
- Module: Elevator system
- Kind: Lesson
- Reading time: 14 min
- Runtime: OpenJDK 18
How do you code an elevator system in a machine-coding round?
Give each Elevator its floor, a direction (up, down or idle) and a sorted set of stops, and let a LOOK rule move it: keep the direction while a stop lies ahead, turn when none does, go idle when none is left. A Dispatcher sends each hall call to the nearest car that is idle or already heading towards it.
Lesson
An elevator is the machine-coding problem where an object model has to carry a scheduling rule. The exercises grow it the way a round does: one car first, then two cars and a dispatcher. The elevator system note has the full design; this lesson is the part you type.
The classes
- Elevator:
floor,dirandstops, aTreeSet<Integer>. Sorted, so the next stop above isstops.higher(floor)and below isstops.lower(floor); a repeated call vanishes on its own. - Direction: an enum
UP,DOWN,IDLE. It is the car's state: it decides which stops count as ahead. - Dispatcher: owns the cars and answers hall calls. A car call, pressed inside, skips it and goes straight to its car.
- Scheduler: the rule for the next stop, behind an interface so LOOK, SCAN or first come first served can be swapped (Strategy).
One tick at a time
A simulation moves time in ticks. On each STEP a car does exactly one thing:
- No stops: it is idle and says so.
- A stop at the floor it is on: the doors open, nothing moves.
- Otherwise it moves one floor in its direction, and stops if that floor was asked for.
Then LOOK settles the direction: keep it while a stop lies ahead, turn when none does, go idle when none is left.
6 2 · CALL 5 · CALL 0 · STEP · CALL 4 · STEP · CALL 3 · STEP …- The car is idle at floor 2. CALL 5 gives it a direction, up, at once; CALL 0 is a second stop, below it.
- STEP: a stop lies ahead at 5, so the car moves up one floor. Meanwhile CALL 4 comes in, ahead of the car, and joins this sweep.
- STEP: the car moves to 4, which is a stop, so it is served in the same tick. CALL 3 arrives now, behind the car: it must wait.
- STEP: up to 5, the last stop above. Nothing lies ahead any more, so LOOK turns the car down as it stops, instead of riding on to the top.
- STEP: down to 4. It was served on the way up, so the car passes it; the stops ahead are 3 and 0.
- STEP: down to 3, the call that came in behind the car. It waited exactly one sweep.
- STEP: down to 2, no one asked for it, so the car keeps moving.
- STEP: down to 1, the last floor before the remaining stop.
- STEP: down to 0, the last stop. With no stops left the car goes idle; every call was served within two sweeps.
The traps
- Nearest stop first. Short to write, and it starves: a busy cluster near the car keeps winning while a far floor waits for ever. LOOK never turns while a stop lies ahead, so a call behind the car waits one sweep at most. In the figure, the call for 3 came in behind the car; it waited for the turn and was served on the way down.
- The idle car. Give it a direction the moment it takes its first call, or the dispatcher still sees it as free and keeps piling calls on it.
- Moving in the wrong place. The dispatcher chooses, the car moves. A dispatcher that edits floors is the mixed responsibility interviewers look for.
Dispatching
The dispatcher's rule, in order:
- Among cars that are idle or already heading towards the floor, take the nearest.
- If none is, take the nearest car anyway; it serves the call after it turns.
- Ties go to the lower-numbered car, so the output is deterministic.
A car call never goes through these steps: it belongs to the car it was pressed in, even when the floor is behind it.
Common questions
Why store an elevator's stops in a TreeSet?
A TreeSet keeps the stops sorted and drops duplicates, so "is there a stop above me?" is one higher(floor) call and "below me?" one lower(floor), each O(log n). A list needs a scan, and a queue serves calls in arrival order and zigzags.
When does a LOOK elevator change direction?
Only when no stop lies ahead in its current direction. A call behind the car waits until the sweep it is on ends, which stops the car zigzagging and still reaches every floor within two sweeps.
What is the difference between a hall call and a car call in code?
A hall call goes to the Dispatcher, which chooses an elevator; a car call is pressed inside one elevator and goes straight into that elevator's stops. In code that is two entry points, such as dispatcher.call(floor) and elevator.press(floor).
Why does an idle elevator need a direction as soon as it is assigned a call?
The dispatcher reads direction to decide who answers the next call. A car left idle after taking a call still looks free, so the next call may be piled onto it, or sent the wrong way, before it has even moved.
Exercises
One car under LOOK
Simulate one elevator, one floor per tick, under LOOK.
Input. The first line is top start: the floors are 0 to top and the car starts idle at start. Then one command per line:
CALL f: add a stop at floorf. Outside0..topprintinvalid floor f. A stop that is already there changes nothing. An idle car takes the direction of its first call at once (up iffis above it, down if below). Prints nothing otherwise.STEP: one tick. With no stops printidle at F. If the floor the car is on is a stop, serve it without moving:stop at F. Otherwise move one floor in the car's direction and printUP to ForDOWN to F, adding: stopwhen that floor is a stop (it is served and removed). After the tick, LOOK settles the direction: no stops left meansIDLE; no stop ahead in the current direction means the car turns.STATUS:at F DIR stops a b cwith the stops in ascending order, orstops none.
Example
6 2
CALL 5
CALL 0
STEP
STEP
STATUS
prints
UP to 3
UP to 4
at 4 UP stops 0 5Two cars and a dispatcher
Two elevators, E1 and E2, each moving exactly as in the first exercise, and a dispatcher that answers hall calls.
Input. The first line is top a b: floors 0 to top, E1 idle at a, E2 idle at b. Then one command per line:
CALL f: a hall call. A car heads towardsfwhen it is idle, goingUPwithfat or above its floor, or goingDOWNwithfat or below it. Choose the nearest car that heads towardsf; if none does, the nearest car. A tie goes toE1. Add the stop to that car (an idle car takes the call's direction, as before) and printcall f -> E1. A floor outside0..topprintsinvalid floor f.PRESS E f: a car call, added straight to that car's stops. Prints nothing, orunknown elevator E, orinvalid floor f(the car is checked first).STEP: one tick forE1, thenE2; each prints its line from the first exercise prefixed with its id, such asE1 UP to 3orE2 idle at 9.STATUS: both cars' status lines, prefixed the same way:E1 at 3 UP stops 6.
Example
10 0 8
CALL 3
CALL 9
STEP
prints
call 3 -> E1
call 9 -> E2
E1 UP to 1
E2 UP to 9: stop
E1 got 3 (both idle, E1 nearer). For 9, E1 heads towards it but E2 is idle and nearer.
In this module: Elevator system
← Checkpoint — Vending machine · Checkpoint: Elevator system →