Start learning free

LESSON 641 OF 642 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Elevator in Rust

Practice O(1)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build Elevator. It moves up and down a building, one floor at a time. It stops at the floors people ask for.

The floors are numbered from 0 to floors - 1. The elevator starts on floor 0, going up.

Implement the class:

  • Elevator::new(floors) makes the elevator. The building has floors floors.
  • request(floor) asks the elevator to stop at floor. It returns nothing.
  • step() moves the elevator one floor, or not at all. It returns the floor the elevator is on afterwards.

Rust has no classes. Elevator is a struct, and its methods sit in an impl block. The function new in that block makes one.

The elevator is always going up or going down. A request is ahead if it is above the elevator while it goes up. Going down, a request is ahead if it is below.

Each step() follows these rules, in this order:

  1. With no requests, nothing happens. The elevator stays where it is.
  2. If no request is ahead, the elevator turns around.
  3. It moves one floor in its direction.
  4. If the new floor was requested, the elevator stops there. That request is done.

So the elevator keeps its direction while a request is ahead. It also keeps its direction while it waits with no requests. Stopping takes no extra step.

A request for a floor outside the building is ignored. So is a request for a floor that is already requested. A request for the floor the elevator is on is done at once.

The calls run in order on one new Elevator. The output lists what the step calls return, in order.

 

Example 1:

Input:
    Elevator::new(6)
    request(3)
    step()
    step()
    request(1)
    step()
    step()
    step()
    step()
Output: 1 2 3 2 1 1
Explanation:
The first two step() calls go up to floor 2.
Then request(1) asks for floor 1. It is below, but floor 3 is still ahead.
So the elevator keeps going up. It stops at floor 3.
Now no request is ahead. The fourth step() turns around and goes down to 2.
The fifth step() stops at floor 1.
No requests are left. So the last step() stays on floor 1.

Example 2:

Input:
    Elevator::new(10)
    request(2)
    step()
    step()
    step()
    request(1)
    request(4)
    step()
    step()
    step()
    step()
    step()
Output: 1 2 2 3 4 3 2 1
Explanation:
The elevator stops at floor 2. No requests are left, so the third step() stays there.
The elevator still faces up.
Then floors 1 and 4 are requested.
Floor 1 is nearer, but floor 4 is ahead.
So the elevator goes up to 4 first. Then it turns around and goes down to 1.

Example 3:

Input:
    Elevator::new(5)
    request(0)
    request(9)
    step()
    request(3)
    request(3)
    step()
    step()
    step()
    request(1)
    step()
    step()
Output: 0 1 2 3 2 1
Explanation:
request(0) asks for the floor the elevator is on. It is done at once.
Floor 9 is outside the building, so request(9) is ignored.
So the first step() has no request, and the elevator stays on floor 0.
The second request(3) changes nothing.
After floor 3, only floor 1 is left. It is below, so the elevator turns around.

 

Constraints:

  • floors is a whole number from 1 to 100,000.
  • Each requested floor is a whole number from -100,000 to 200,000.
  • At most 100,000 calls follow the constructor.

Note:

Each call should run in O(1) time. Looking through every request on each step would take O(n) time, for n requests.

STEP 1 OF 20
    Ctrl + Enter to check

    Elevator in Rust

    Keep going with Rust500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free