Nearest K Street View Cars to a Dispatch Point
Last updated: November 29, 2026
Quick Overview
Given the planar coordinates of Street View capture cars and an integer k, return the k cars closest to the origin, which is the dispatch depot.
0
0
0 solved
Given the planar coordinates of Street View capture cars and an integer k, return the k cars closest to the origin, which is the dispatch depot.
Framed around Street View fleet dispatch, this is a staple Google screen question for checking that a candidate reaches for a bounded heap and avoids square roots. The interviewer also listens for the quickselect alternative and its trade-offs.
What the Interviewer Expects
- Compares squared distances instead of calling sqrt
- Uses a max-heap of size k (or quickselect)
- States O(n log k) time
- Clarifies tie handling and output order
- Mentions average O(n) quickselect as an option
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 do this if the cars stream in continuously?
- What if the dispatch point moves?
- How would you answer this on a sphere using lat/lng?
- How would a spatial index change things for many queries?
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: list of
(x, y)integer points andkwith1 <= k <= n. - Output: any k points with the smallest Euclidean distance to
(0, 0), in any order. - Ties at the boundary can be broken arbitra...
High-Level Design
Distance ordering is the same for squared distance, so compare x*x + y*y and skip floating point entirely. Maintain a max-heap of the k closest points seen so far; Python only ships a min-heap, so p...