Algo-Sim › Low-Level Design

Elevator System (LOOK scheduling)

Medium · Low-Level Design · step through it in Java, Python and C++ on your own input

One elevator serving floor requests with LOOK: keep moving in one direction while stops remain ahead, then reverse. Two sorted sets give the next stop in O(log n).

Problem

Design an elevator controller. The car starts at floor 0 going up. request(f) records a stop at floor f. next() moves the car to its next stop and returns that floor: the nearest stop ahead in the current direction, or, if there is none, the nearest stop in the other direction after turning around. With no stops waiting it stays put. Return the floor after every next().

Constraints

Edge cases to test

Follow-up questions

How would you schedule several elevators?
Assign each request to the elevator with the lowest estimated arrival time, accounting for its direction and stops.
Why LOOK instead of first-come-first-served?
FCFS zigzags across floors; LOOK serves everything in one direction first, cutting travel.

Solution

Java

class Elevator {
    private int floor = 0, dir = 1;                             // dir: 1 up, -1 down
    private final TreeSet<Integer> up = new TreeSet<>();        // stops above the car
    private final TreeSet<Integer> down = new TreeSet<>();      // stops below the car

    public void request(int f) {
        if (f > floor) up.add(f);
        else if (f < floor) down.add(f);
    }

    /** LOOK: keep going while there are stops ahead, then turn around. */
    public int next() {
        if (up.isEmpty() && down.isEmpty()) return floor;
        if (dir == 1 && up.isEmpty()) dir = -1;
        else if (dir == -1 && down.isEmpty()) dir = 1;
        floor = dir == 1 ? up.pollFirst() : down.pollLast();
        return floor;
    }
}

Python

import bisect


class Elevator:
    def __init__(self):
        self.floor, self.dir = 0, 1         # dir: 1 up, -1 down
        self.up, self.down = [], []         # sorted stops above / below the car

    def request(self, f):
        if f > self.floor and f not in self.up:
            bisect.insort(self.up, f)
        elif f < self.floor and f not in self.down:
            bisect.insort(self.down, f)

    # LOOK: keep going while there are stops ahead, then turn around.
    def next(self):
        if not self.up and not self.down:
            return self.floor
        if self.dir == 1 and not self.up:
            self.dir = -1
        elif self.dir == -1 and not self.down:
            self.dir = 1
        self.floor = self.up.pop(0) if self.dir == 1 else self.down.pop()
        return self.floor

C++

#include <bits/stdc++.h>
using namespace std;

class Elevator {
  public:
    int floor = 0, dir = 1; // dir: 1 up, -1 down
    set<int> up = {};       // stops above the car
    set<int> down = {};     // stops below the car

    void request(int f) {
        if (f > floor)
            up.insert(f);
        else if (f < floor)
            down.insert(f);
    }

    /** LOOK: keep going while there are stops ahead, then turn around. */
    int next() {
        if (up.empty() && down.empty())
            return floor;
        if (dir == 1 && up.empty())
            dir = -1;
        else if (dir == -1 && down.empty())
            dir = 1;
        if (dir == 1) {
            auto it = up.begin();
            floor = *it;
            up.erase(it);
        } else {
            auto it = prev(down.end());
            floor = *it;
            down.erase(it);
        }
        return floor;
    }
};

More Low-Level Design problems