Java Design Program · CD25
Design an Elevator System in Java
An elevator that always goes to the nearest request can leave someone waiting forever. Serving every request in the current direction before turning around is fair and efficient.
Problem Statement
Simulate one elevator that moves one floor per tick. Requests arrive as (tick, floor). The elevator stops at a floor when it reaches a pending request for it.
It keeps moving in its current direction while there are requests ahead, reverses when there are none, and waits when there are no requests at all.
Input/Output Examples
at floor 5 going up; requests 7, 3, 9Outputstops at 7, 9, then 3Explanation: floors above are served before turning.floor 6 requested at t=3, when the car is at 8Outputserved on the way downExplanation: it is behind the car when it arrives.a request for the current floorOutputstops at onceExplanation: no movement is needed.Visual Explanation
- t=0floor 5 ↑793requests ahead: 7, 9
- t=2stop7keep going up
- t=4stop9nothing above: reverse
- t=7, t=10stops63serve the rest going down
- Done7, 9, 6, 3
This is the LOOK algorithm, named after disk scheduling: like SCAN, but the car turns at the last request instead of at the top floor.
Approaches
Both approaches produce the same stops for 20,000 random request sequences. They differ in how they find the next request ahead.
Approach 1: TreeSet of Pending Floors (Recommended)
Idea: keep every pending floor in a TreeSet; higher(floor) or lower(floor) tells you whether any request is ahead.
- Add the requests that arrive at this tick.
- If the current floor is pending, remove it and record a stop.
- If nothing is ahead in the current direction, reverse.
- Move one floor and advance the tick.
Time: O(log n) · Space: O(n). per tick; n is the number of pending requests.
Approach 2: Up and Down Heaps
Idea: put floors above the car in a min-heap and floors below it in a max-heap; serve one heap until it is empty, then switch direction.
- Send a new request to the up heap if it is above the car, to the down heap if below, or stop now if equal.
- Stop when the top of the heap in use equals the current floor.
- Reverse when the heap for the current direction is empty.
Time: O(log n) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: TreeSet of Pending Floors (Recommended)
One sorted set answers every question the scheduler asks.
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class ElevatorTreeSet {
// LOOK scheduling: keep going in one direction while requests remain ahead, then turn around.
// Each tick: accept new requests, stop if this floor was requested, then move one floor.
static List<String> run(int floor, List<int[]> requests) {
TreeSet<Integer> pending = new TreeSet<>();
List<String> stops = new ArrayList<>();
int dir = 1, next = 0;
for (int t = 0; next < requests.size() || !pending.isEmpty(); t++) {
while (next < requests.size() && requests.get(next)[0] == t) {
pending.add(requests.get(next++)[1]);
}
if (pending.remove(floor)) {
stops.add("t=" + t + ": stop at floor " + floor);
}
if (pending.isEmpty()) continue; // idle until the next request
boolean ahead = dir > 0 ? pending.higher(floor) != null : pending.lower(floor) != null;
if (!ahead) dir = -dir; // nothing ahead: reverse
floor += dir;
}
return stops;
}
public static void main(String[] args) {
// Start at floor 5 going up. Requests 7, 3 and 9 arrive at once; floor 6 is requested at t=3.
List<int[]> requests = List.of(new int[]{0, 7}, new int[]{0, 3}, new int[]{0, 9}, new int[]{3, 6});
List<String> stops = run(5, requests);
stops.forEach(System.out::println);
}
}
t=2: stop at floor 7 t=4: stop at floor 9 t=7: stop at floor 6 t=10: stop at floor 3
Time complexity: O(log n) · Space complexity: O(n).
Approach 2: Up and Down Heaps
A common interview answer; the heap tops are always the next stops in each direction.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
public class ElevatorTwoHeaps {
// Requests above the car go into a min-heap (next stop going up), requests below into a max-heap
// (next stop going down). The car serves one heap until it is empty, then switches.
static List<String> run(int floor, List<int[]> requests) {
PriorityQueue<Integer> up = new PriorityQueue<>();
PriorityQueue<Integer> down = new PriorityQueue<>(Collections.reverseOrder());
List<String> stops = new ArrayList<>();
int dir = 1, next = 0;
for (int t = 0; next < requests.size() || !up.isEmpty() || !down.isEmpty(); t++) {
boolean stopHere = false;
while (next < requests.size() && requests.get(next)[0] == t) {
int f = requests.get(next++)[1];
if (f > floor) up.add(f); else if (f < floor) down.add(f); else stopHere = true;
}
while (!up.isEmpty() && up.peek() == floor) { up.poll(); stopHere = true; }
while (!down.isEmpty() && down.peek() == floor) { down.poll(); stopHere = true; }
if (stopHere) {
stops.add("t=" + t + ": stop at floor " + floor);
}
if (up.isEmpty() && down.isEmpty()) continue;
if (dir > 0 && up.isEmpty()) dir = -1;
else if (dir < 0 && down.isEmpty()) dir = 1;
floor += dir;
}
return stops;
}
public static void main(String[] args) {
// Start at floor 5 going up. Requests 7, 3 and 9 arrive at once; floor 6 is requested at t=3.
List<int[]> requests = List.of(new int[]{0, 7}, new int[]{0, 3}, new int[]{0, 9}, new int[]{3, 6});
List<String> stops = run(5, requests);
stops.forEach(System.out::println);
}
}
t=2: stop at floor 7 t=4: stop at floor 9 t=7: stop at floor 6 t=10: stop at floor 3
Time complexity: O(log n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| TreeSet of Pending Floors | O(log n) | O(n) |
| Up and Down Heaps | O(log n) | O(n) |
Each new request costs O(log n) to insert, and each tick needs at most a few set or heap operations.
Interview Tips and Recommended Approach
- Clarify the scope: one car or many, inside buttons or hall calls with a direction.
- Explain why not nearest-first: it can starve far floors when nearby requests keep arriving.
- Separate the scheduler from the car, so a dispatcher can assign hall calls to many cars.
- Model states such as MOVING_UP, MOVING_DOWN, IDLE and DOORS_OPEN for a fuller design.
Recommended Approach: TreeSet of Pending Floors
Why: one sorted set answers both questions the scheduler asks, "stop here?" and "anything ahead?", with less code than two heaps.