Elevator System Design: Acing the Object-Oriented Design Interview
4 views
"Design an elevator system" is one of the classic object-oriented design (OOD) interview questions. Unlike large-scale distributed design, the interviewer wants to see clean abstractions, sensible responsibilities, and a scheduling strategy you can justify.
Clarify the scope first
Ask: how many elevators and floors? Are there special elevators (freight, express)? Do we need to handle emergencies, maintenance mode, weight limits, and door sensors? Is the goal to minimize wait time, travel time, or energy? A reasonable scope for 45 minutes: a building with N floors and M elevators, hall call buttons (up/down) on each floor, car buttons inside each elevator, and a central controller that assigns requests.
Identify the core classes
- Building: holds floors and the elevator controller.
- Floor: has an up button and a down button (except top and bottom floors) and a display.
- ElevatorCar: current floor, direction (UP, DOWN, IDLE), state (MOVING, STOPPED, DOORS_OPEN, MAINTENANCE), capacity, and a set of target floors.
- Request: either a HallRequest(floor, direction) or a CarRequest(car_id, destination_floor).
- ElevatorController (or Dispatcher): receives requests and assigns hall calls to cars.
- SchedulingStrategy: an interface, so different algorithms can be swapped in.
Interviewers like the Strategy pattern here because it shows you anticipate changing requirements.
Scheduling: the heart of the question
Explain at least two strategies and their trade-offs:
1. First-come, first-served: simple but terrible for efficiency; cars zigzag.
2. SCAN / elevator algorithm (also called LOOK): a car keeps moving in its current direction serving all requested floors along the way, then reverses only when no requests remain ahead. This mirrors real elevators and prevents starvation.
3. For assigning hall calls among multiple cars, compute a cost per car: for example the number of floors it must travel to reach the caller, adding a penalty if the car is moving away or is nearly full. Pick the lowest-cost car.
Keep two sorted structures per car: upward stops in a min-heap and downward stops in a max-heap. That makes "next stop" lookups efficient.
State transitions
Draw a small state machine: IDLE to MOVING when a target exists; MOVING to STOPPED on arrival; STOPPED to DOORS_OPEN; DOORS_OPEN back to MOVING or IDLE after a timeout. Mention that door-obstruction sensors keep doors open, and overweight prevents departure.
Concurrency
Requests arrive from many buttons simultaneously. The controller can process them through a thread-safe queue, while each car runs its own loop. Mention synchronization around each car's stop sets.
Edge cases interviewers ask about
- Fire mode: all cars return to the lobby and ignore calls.
- Maintenance: remove a car from scheduling.
- Peak hours: in the morning, park idle cars at the lobby; in the evening, distribute them across upper floors.
Present it well
Start with classes, then the scheduling strategy, then edge cases. Sketch a few method signatures like assignRequest(HallRequest) and step(). Practicing out loud helps you avoid getting lost in details. Candidates who want extra support can try Linkjob AI (https://linkjob.ai), which offers real-time help during job interviews and technical interviews, so you keep a clear structure when the interviewer adds new requirements mid-way.