Simulate Nearest Idle Driver Dispatch Along a Highway
Last updated: November 29, 2026
Quick Overview
Drivers sit at integer positions on a one-dimensional highway and ride requests arrive over time with a pickup and dropoff point. Assign each request to the nearest idle driver (lower driver id on ties), queue requests in FIFO order when no one is idle, and return the driver id assigned to each request.
Uber
November 29, 20260
0
0 solved
Drivers sit at integer positions on a one-dimensional highway and ride requests arrive over time with a pickup and dropoff point. Assign each request to the nearest idle driver (lower driver id on ties), queue requests in FIFO order when no one is idle, and return the driver id assigned to each request.
A simulation question in the style of Uber's onsite coding round, a stripped down version of the dispatch loop. The interviewer checks event ordering (who frees up before whom), a min-heap for busy drivers, and careful handling of queued requests.
What the Interviewer Expects
- Defines trip duration and driver end position precisely
- Uses a min-heap keyed by free time for busy drivers
- Releases drivers in time order and serves the queue at each release
- Applies the tie rule consistently
- Discusses how a 2D map changes the nearest lookup
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you speed up the nearest idle lookup with thousands of drivers?
- What if a queued rider cancels after a timeout?
- How would you batch requests arriving in the same second for a better global match?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: drivers (list of positions, index is driver id) and requests sorted by time, each (t, pickup, dropoff). A driver at p assigned at time T finishes at `T + |p - pickup| + |dropoff - pic...
High-Level Design
Keep an idle set (id -> position), a heap of busy drivers (free_time, id, end_pos), and a FIFO of waiting requests. Before handling a request at time t, release busy drivers with `free_time <= t...