Dispatch Incoming Orders to the Nearest Free Delivery Partner
Last updated: November 29, 2026
Quick Overview
Orders arrive in time order, each with a restaurant and a customer location on a grid. Assign each order to the free delivery partner closest to the restaurant (ties by id); if nobody is free, wait for the earliest partner to finish, and report every order's pickup and delivery time.
Swiggy
November 29, 20260
0
0 solved
Orders arrive in time order, each with a restaurant and a customer location on a grid. Assign each order to the free delivery partner closest to the restaurant (ties by id); if nobody is free, wait for the earliest partner to finish, and report every order's pickup and delivery time.
Rider assignment is the core of Swiggy's last-mile system, and a simplified greedy dispatcher is a natural coding round for backend candidates. The interviewer checks event-ordered simulation, correct use of a heap for riders becoming free, and whether you can reason about where the greedy rule is weak.
What the Interviewer Expects
- Keeps busy riders in a min-heap keyed by the time they become free
- Releases every rider whose free time is at or before the order time before choosing
- Handles the no-free-rider case by advancing time to the earliest completion
- Updates the rider's location to the customer drop point after delivery
- Breaks ties deterministically
- Explains the complexity and how a spatial index would replace the linear scan
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
- With 50,000 riders in a city, how do you find the nearest free one without scanning everyone?
- How would you batch two orders from the same restaurant onto one rider?
- Greedy first-come assignment can be globally poor. How would a short batching window with bipartite matching help?
- Riders reject orders. How does the simulation change?
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: riders mapping id to start location (all idle at t=0) and orders as (order_id, t, restaurant, customer) sorted by t. Travel time equals Manhattan distance. For each order pick the free ...
High-Level Design
Two pools: free (dict of idle riders and their location) and busy (min-heap of (free_at, rider_id, drop_location)). For each order: pop all riders with free_at <= t back into free. If free...