Skip to content
LLD Learn/Case Studies: Physical Systems
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Design an Elevator System

4 min read

You'll learn to

  • -Name the classes of an elevator system and the two kinds of request: a hall call and a floor button inside the car
  • -Explain SCAN scheduling as "keep going the same way, then turn around" and run it against nearest-first
  • -Choose an elevator for a hall call with a simple, defensible rule, and say out loud what it ignores

The elevator prompt tests something the parking lot does not: a genuinely interesting scheduling decision, and coordination between several elevators that compete to serve the same building. It rewards candidates who resist over-engineering the scheduler in the first pass.

Step 1: Requirements and Core Classes

Assume a building with several elevators. Each has a current floor and a direction. Requests come from two places. A passenger inside presses a floor button. A person waiting on a floor presses up or down; this outside press is called a hall call. A dispatcher decides which elevator takes which hall call.

The core class shape
from enum import Enum

class Direction(Enum):
    UP = 1
    DOWN = -1
    IDLE = 0

class Request:
    def __init__(self, floor: int, direction: Direction | None = None):
        self.floor = floor
        self.direction = direction   # None for an internal "go to floor" request

class Elevator:
    def __init__(self, elevator_id: int):
        self.id = elevator_id
        self.current_floor = 0
        self.direction = Direction.IDLE
        self._requests: set[int] = set()   # floors this elevator must stop at

    def add_request(self, floor: int) -> None:
        self._requests.add(floor)

    def step(self) -> None:
        if not self._requests:
            self.direction = Direction.IDLE
            return
        target = min(self._requests, key=lambda f: abs(f - self.current_floor))
        self.direction = Direction.UP if target > self.current_floor else Direction.DOWN
        self.current_floor += self.direction.value
        if self.current_floor in self._requests:
            self._requests.remove(self.current_floor)   # arrived, doors open

Step 2: SCAN Scheduling as a Strategy

Think of a street sweeper on a one-way route, or a bus that keeps going down the line. Real elevators do not jump to whichever request is nearest. They use SCAN, or its slightly more efficient cousin LOOK. The elevator keeps moving in its current direction, stops for every request on the way, and turns around only when nothing remains ahead. That stops the elevator zig-zagging if requests keep arriving just behind it, and no request waits forever, because the elevator sweeps the whole building. How the elevator picks its next stop is exactly the kind of thing that should be swappable, so it is a natural fit for the Strategy pattern.

SCAN as a swappable SchedulingStrategy
class SchedulingStrategy(ABC):
    @abstractmethod
    def next_floor(self, elevator: Elevator) -> int | None: ...

class ScanStrategy(SchedulingStrategy):
    def next_floor(self, elevator: Elevator) -> int | None:
        requests = elevator._requests
        if not requests:
            return None
        if elevator.direction != Direction.DOWN:
            ahead = [f for f in requests if f >= elevator.current_floor]
            if ahead:
                return min(ahead)          # continue up, picking up requests along the way
        behind = [f for f in requests if f <= elevator.current_floor]
        if behind:
            return max(behind)             # no more requests ahead - reverse direction
        return min(requests, key=lambda f: abs(f - elevator.current_floor))

Step 3: Dispatching Among Several Elevators

For a hall call, the dispatcher must pick one of N elevators. A reasonable first pass is to prefer an idle elevator, and among those the closest, and fall back to the closest of all if none is idle. That avoids sending a busy elevator away from its riders when a closer idle one is available.

A simple, defensible dispatch heuristic
class Dispatcher:
    def __init__(self, elevators: list[Elevator]):
        self._elevators = elevators

    def dispatch(self, request: Request) -> Elevator:
        idle = [e for e in self._elevators if e.direction == Direction.IDLE]
        candidates = idle or self._elevators   # prefer idle; fall back to all
        best = min(candidates, key=lambda e: abs(e.current_floor - request.floor))
        best.add_request(request.floor)
        return best

Step 4: Run It and Read the Trade-Off

The demo gives one elevator, standing at floor 5 and heading up, the requests 4, 7 and 9. It runs once with a nearest-first rule and once with SCAN, then dispatches a hall call at floor 7 among three elevators.

Press Run: SCAN travels further, but never turns a rider around mid-trip

Nearest-first travels fewer floors, 6 against 9, because it grabs floor 4 first. But that means the elevator turns back toward floor 4 while the riders heading up to 7 and 9 wait. SCAN gives up some distance in return for predictability and fairness. Say that trade-off aloud; do not pretend the simplest rule wins on every measure.

A strong candidate names the weakness of the dispatcher rather than pretending it is optimal: "closest idle first is deliberately simple; a production system would also weigh current load and how long each passenger has waited, but it is a defensible, easy-to-reason-about first pass."

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Forge Level 39: Elevator System asks you to implement a Building/Elevator dispatch system with nearest-idle-first assignment, graded by running your actual code against real test cases.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!