Elevator System LLD Quiz: LOOK, SCAN and Dispatch Practice
Practise the elevator machine-coding problem with ten design questions and a Java program that runs LOOK, SCAN and first come first served as strategies.
- Course: Low-Level Design study plan
- Module: Elevator system
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: OpenJDK 18
Checkpoint: Elevator system is the checkpoint that closes the Elevator system module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the module: the elevator's classes, direction as state, LOOK, dispatching hall calls, and the scheduling policy as a Strategy.
How it works. Ten questions and one program. You need 70% on the questions and the program accepted to clear the module. Retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What does a LOOK car do when no stop lies ahead of it?
- How is SCAN different from LOOK, and when do they travel the same distance?
- Which cars may the dispatcher pick first, and what happens when none qualifies?
- Why does a car call skip the dispatcher?
- Where does the scheduling rule live, so a new policy changes no other class?
The program is the Strategy pattern at work: three policies behind one interface, the same stops served three ways. Work one line through by hand before you code it; the turn at the end of the shaft is where most wrong answers come from.
Common questions
Is LOOK always better than SCAN?
LOOK travels no further than SCAN on the same stops, because it skips the empty ride to the end of the shaft. SCAN's fixed sweep makes the worst-case wait easier to bound, which is why textbooks still teach both.
Which design patterns does an elevator system use?
Strategy for the scheduling policy, State for the direction (and door states in a fuller design), and Observer for floor displays that follow each car. The dispatcher is sometimes a Singleton, though injecting it is easier to test.
How do you make an elevator simulation testable?
Drive it with explicit ticks instead of real time, break every tie by a fixed rule such as the lower car number, and print each decision. The same script then always produces the same output, which a test can compare line by line.
Exercise
Three scheduling policies
Put LOOK, SCAN and first come first served behind one Scheduler interface and serve the same stops with each.
Input. The first line is top (floors 0 to top). Each following line is POLICY start DIR s1 s2 …: a policy name, the car's floor, its direction (UP or DOWN) and the stops in the order they were pressed. Stops are distinct and differ from start.
Policies
LOOK: serve every stop ahead in the current direction, nearest first; then turn and do the same.SCAN: the same, but when stops remain after a sweep, ride on to the end of the shaft (topgoing up,0going down) before turning. A car already at that end does not ride.FCFS: the stops in the order given.
Output. One line per input line: POLICY: v1 v2 … (N floors), the floors in the order visited, SCAN's ride to an end written in brackets like [10], and N the floors travelled from start. An unknown policy prints unknown policy NAME.
Example
10
LOOK 4 UP 7 3 9 6
SCAN 4 UP 7 3 9 6
prints
LOOK: 6 7 9 3 (11 floors)
SCAN: 6 7 9 [10] 3 (13 floors)In this module: Elevator system
← Elevator system: one car, then a dispatcher · Movie ticket booking: holds, payments and the race →