Design an elevator system for a building with N lifts. When someone presses the call button, which lift answers, and where in your code is that decided?
An elevator system splits into a state machine per lift that decides its own next floor, and a dispatcher that assigns each hall call to one lift through a pluggable strategy such as a nearest-suitable-car cost. Car buttons go straight to their lift, and one control loop owns all state, so no god class and no locks.
Answer
Short answer
Split the decision in two. In an elevator system, each lift is a state machine that owns its position, direction and stop list, and decides on its own which floor comes next. A separate dispatcher answers the hall call: when someone presses up or down on a landing, it asks a strategy to score every in-service lift and adds the stop to the cheapest one. Buttons pressed inside a car skip the dispatcher entirely, because that passenger has already chosen their lift.
Was this answer helpful?
Likely follow-ups
- Lift 2 was assigned a call, then a closer lift became free. Do you reassign, and what has to be undone?
- The building wants two lifts reserved for floors 20 to 40 at peak hours. Which classes change?
- How would you change the cost function so a lift with eight stops ahead is not picked over an idle one two floors further away?
- A lift faults with passengers inside and three hall calls assigned to it. Walk me through what happens to each.
Related questions
- Design a food-ordering system: customers place orders with restaurants, and the system assigns a delivery partner. Walk me through the classes.mediumAlso on state-machine and machine-coding5 min
- Design a movie ticket booking system with shows, seats and a temporary hold that expires if the user does not pay. Write the classes.hardAlso on machine-coding and state-machine6 min
- Design a parking lot system — you have 90 minutes, working code at the end.hardAlso on machine-coding and strategy-pattern6 min
- Build an in-memory pub-sub with topics, many subscribers per topic and at-least-once delivery. How does a message get redelivered?hardAlso on machine-coding6 min