KatInterview

Modeling a Multi-Car Lift Dispatcher with Swappable Policies

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designstate-machinestrategy-pattern

Last updated

Design an object-oriented lift (elevator) control system: classes, per-car state machine, stop ordering, pluggable dispatch, edge cases, and testable core logic.

You are asked to design the control software for a lift (elevator) system in a building, using object-oriented principles. Explain your class model, then show how a single button press ends with a car stopping at the requested floor, and write the core control logic in TypeScript or another language you prefer.

Baseline setup (confirm or adjust, then state your defaults):

  • The building has floors from a lowest to a highest number and one or more cars.
  • Every floor has hall buttons for up and down. The bottom floor only has up, and the top floor only has down.
  • Every car has an internal panel of floor buttons.
  • A central controller chooses which car serves each hall call. A car moves one floor per step, stops, and opens its doors.

Constraints:

  • Model the physical world as a step-based simulation (a tick() or step() call) so the logic can be tested with no hardware and no real clock.
  • Scheduling goals, special modes, and capacity rules are not fixed. Pick defaults and say what they are.

Questions you should consider asking:

  • How many floors and cars must be supported? Should it work with one car as well as a group?
  • What is being optimized: waiting time, total travel time, or energy?
  • Are there weight or capacity limits, and what happens when a car is full?
  • Which special modes matter (maintenance, emergency stop, fire service)?
  • What happens to requests already assigned to a car that goes out of service?

Expected in your answer:

  1. Requirements and scope.
  2. Classes and their responsibilities.
  3. A car state model with valid transitions.
  4. A stop-ordering policy within a car and a separately swappable car-assignment policy.
  5. Edge cases: duplicate presses, a request for the current floor, invalid floors, and a car failing while holding requests.
  6. Code for the core logic, plus a note on extensibility and testing.

Hints

Hint 1

Separate three concerns: who decides which car answers a call (dispatcher), how one car orders its own stops (car), and how input arrives (button panels producing request objects).

Hint 2

Give each car an explicit state (idle, moving up, moving down, doors open, out of service) and keep two sorted sets of pending stops, one for floors above and one for floors below. Keep the current direction until nothing remains ahead, then reverse.

Hint 3

Make the assignment policy an interface so you can swap nearest-car for something smarter, and drive everything through a step function so tests can assert exact floor sequences. Decide up front what happens to duplicates and to a request for the floor the car is already at.