Assign Incoming Orders to the First Free Dasher With Heaps
Last updated: November 29, 2026
Quick Overview
Given n Dashers and orders as (arrival, duration) sorted by arrival, assign each order to the free Dasher with the lowest id, or if none is free, to the Dasher who frees up earliest (ties by id), delaying the order; return each order's Dasher and per-Dasher counts.
DoorDash
November 29, 20260
0
0 solved
Given n Dashers and orders as (arrival, duration) sorted by arrival, assign each order to the free Dasher with the lowest id, or if none is free, to the Dasher who frees up earliest (ties by id), delaying the order; return each order's Dasher and per-Dasher counts.
A simplified version of the dispatch simulation DoorDash engineers reason about every day. The interviewer is checking two heaps working together (free Dashers by id, busy Dashers by free time), correct release of Dashers before each assignment, and correct handling of delayed starts.
What the Interviewer Expects
- Use one heap for idle Dashers by id and one for busy Dashers by free time
- Release every Dasher whose free time is at or before the arrival
- When none is free, start the order at the earliest free time
- Break ties on free time by Dasher id
- Analyze O(m log n) time
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 add a maximum wait after which an order is rejected?
- How would you prefer the Dasher closest to the store instead of the lowest id?
- How would you support Dashers going offline mid-shift?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
n_dasherswith ids0..n-1, all idle at time 0.- Orders
(arrival, duration)sorted by arrival. - Free Dasher available: lowest id takes it at
arrival. - None free: the Dasher with the earlie...
High-Level Design
Keep free as a min-heap of ids and busy as a min-heap of (free_at, id). Before each order, pop every busy Dasher with free_at <= arrival into free. If free is non-empty, the lowest id star...