PythonStudio
Interview Problems

Elevator System

medium · state machine, SCAN scheduling

Brief-reported interview attribution (unverified): Amazon, Microsoft

How to approach an LLD round

Design one elevator for a building of num_floors floors, numbered 0 to num_floors - 1 (ground floor = 0). The elevator has a current floor, a direction ("up", "down", "idle"), and a set of requested floors. request(floor) adds a floor (returns False for an out-of-range floor, True otherwise). step() advances the elevator by exactly one floor, or serves the floor it is already on. The elevator uses SCAN scheduling: it keeps moving in its current direction while any requested floor lies that way, and only reverses when no requested floor remains in that direction.

class Elevator:
    def __init__(self, num_floors):
    def request(self, floor):
        # -> True if accepted, False if the floor is out of range
    def step(self):
        # advance one floor, or serve the current floor if it is requested
    def status(self):
        # -> {"floor": int, "direction": "up"|"down"|"idle", "pending": sorted list of floors}

Rules. A step() with no pending requests does nothing and leaves the direction "idle". If the elevator is standing on a requested floor, that step serves it (removes the request, doors open) and the elevator does not move. After serving a floor, the direction is "up" if any pending floor is above, "down" if any is below, else "idle". A request for the current floor is served on the next step. When the elevator arrives at a requested floor by moving, the request is cleared in the same step.

Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.

Public demo

# Public demo: run after implementing the class API above.
e = Elevator(21)
print(e.request(21))
print(e.request(-1))

Starter code

class Elevator:

    def __init__(self, num_floors):
        """Initialize the state described in the problem."""
        pass

    def request(self, floor):
        """-> True if accepted, False if the floor is out of range"""
        pass

    def step(self):
        """advance one floor, or serve the current floor if it is requested"""
        pass

    def status(self):
        """-> {"floor": int, "direction": "up"|"down"|"idle", "pending": sorted list of floors}"""
        pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler