00:00

Problem Statement

Input/Output Examples

Visual Explanation

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.

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.

Complexity Analysis

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.