Design Projects: Classic Systems · Coding problem
Serve the Floors in LOOK Order
A free coding problem from the Design an Elevator System skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
An elevator runs the LOOK algorithm. Given the floor it's on, the direction it's heading (UP, DOWN or IDLE) and the floors that have been requested, print the order it stops at them and how many floors it travels.
- Each requested floor is a stop once, however many times it was requested.
- A request for the current floor is served first, without moving.
- Heading
UP: stop at every requested floor above, lowest first; then turn and stop at the ones below, highest first.DOWNis the mirror image. IDLE: head toward the nearest requested floor (a tie goesUP), then continue as above.
Implement List<Integer> stopOrder(int current, String direction, int[] requests) and long travel(int current, List<Integer> stops).
In Python: stop_order(current, direction, requests) returns a list and travel(current, stops) an int. Python has no TreeSet: sort the distinct floors and split them with bisect. The first line of output is the stops separated by spaces (NONE if there are none), the second the floors traveled.
Input: the current floor and the direction; the number of requests k; then the k requested floors.
Example
Input:
5 UP
5
2 8 5 9 3
Output:
5 8 9 3 2
11
Floor 5 is served where the car stands. Heading up, it stops at 8 and 9, then turns and stops at 3 and 2: 0 + 3 + 1 + 6 + 1 = 11 floors.
Constraints
0 <= floor <= 2 * 10^9for the current and every requested floor0 <= k <= 2 * 10^5- the total travel can exceed the range of an
int
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in Design an Elevator System
Solve it Serve the Floors in LOOK Order free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime