~learn.
0:00 / 1:16
LLD · Elevator system

The elevator one floor away is the wrong one

transcript

You're on floor 2. You press down. Car A is one floor away, at 3. Heading up to 8. It's the wrong car.

The controller scores each car. How many floors before it can pick you up? An idle car? Just the distance. Moving your way, with you ahead? Also the distance. You're on its route. Otherwise it must finish its run first. Then come back.

You're behind Car A, going the other way. So it finishes first. 5 floors up to 8. Then 6 floors back down to you. Cost: 11. An idle car farther away can easily be cheaper.

So which car answers is the controller's job. Through a dispatch strategy. The order of stops is the car's own job. It runs LOOK. Test one car with no controller. Swap the dispatcher without touching a car.

Morning peak. Car A takes floors 1 to 5, car B 6 to 9. That's a new dispatch strategy. The cars don't change at all.

Closest isn't cheapest. Count the floors to pick-up.

Full breakdown in the lecture.