Find the K Amazon Lockers Closest to a Customer
Last updated: November 29, 2026
Quick Overview
Given the (x, y) coordinates of a customer and a list of locker locations, return the k lockers with the smallest Euclidean distance, ordered nearest first and breaking ties by locker ID. The list can contain millions of lockers.
Amazon
November 29, 20260
0
0 solved
Given the (x, y) coordinates of a customer and a list of locker locations, return the k lockers with the smallest Euclidean distance, ordered nearest first and breaking ties by locker ID. The list can contain millions of lockers.
A top-k selection problem framed around Amazon Locker pickup suggestions. The interviewer wants to see a bounded heap rather than a full sort, squared distances to avoid floating point, and a sensible tie-break.
What the Interviewer Expects
- Use squared distance, no sqrt needed for comparison
- Keep a max-heap of size k for O(n log k)
- Define a deterministic tie-break
- Compare with quickselect and full sort
- Discuss spatial indexes for repeated queries
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 answer many queries per second against a fixed locker set?
- What if distance should be driving time rather than straight line?
- How would you exclude lockers that are full?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Lockers are tuples (locker_id, x, y). 0 <= k. If k >= number of lockers return all, sorted. Distance ties break by smaller locker_id. Return a list of locker IDs....
High-Level Design
Maintain a max-heap of the k best seen so far. Python's heapq is a min-heap, so push negated keys (-dist2, -id); the root is then the worst of the current best (farthest, and among equal distance th...