Top K Highest Rated Restaurants Within a Delivery Radius
Last updated: November 29, 2026
Quick Overview
Given restaurants as (id, x, y, rating) and a customer location, return the ids of the k highest rated restaurants within a delivery radius, breaking ties by shorter distance and then by smaller id.
DoorDash
November 29, 20260
0
0 solved
Given restaurants as (id, x, y, rating) and a customer location, return the ids of the k highest rated restaurants within a delivery radius, breaking ties by shorter distance and then by smaller id.
A heap question rooted in the DoorDash home feed, where the app must surface a short, well-ordered list of nearby stores. The interviewer checks that you filter before ranking, keep a size-k min-heap instead of sorting everything, and write a tie-break key that is fully deterministic.
What the Interviewer Expects
- Filter by radius before any ranking work
- Use a min-heap of size k so the weakest kept candidate is on top
- Encode the three-level tie break in one comparable key
- Return results best first
- Compare O(n log k) with O(n log n) sorting
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 rank by a blend of rating, distance and predicted ETA?
- What if the restaurants are already indexed in a geohash or quadtree?
- How would you paginate to fetch the next k?
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:
restaurantswith integer ids, customer point(cx, cy),radius, andk. - Keep only restaurants with Euclidean distance
<= radius. - Order: rating descending, then distance ascending,...
High-Level Design
Build a key where a larger key means a better restaurant: (rating, -distance, -id). Python tuples compare left to right, so this captures all three tie breaks. Maintain a min-heap of at most k entri...