Spread Amazon Lockers to Maximize the Minimum Gap Between Them
Last updated: November 29, 2026
Quick Overview
A street has candidate locker sites at given positions. Place exactly m lockers on distinct sites so the smallest distance between any two placed lockers is as large as possible, and return that distance.
Amazon
November 29, 20260
0
0 solved
A street has candidate locker sites at given positions. Place exactly m lockers on distinct sites so the smallest distance between any two placed lockers is as large as possible, and return that distance.
An Amazon onsite binary-search-on-answer problem framed around siting Amazon Lockers so they cover a corridor without clustering. Interviewers check the greedy feasibility proof and that you search for the largest feasible value, the mirror image of the usual lower bound.
What the Interviewer Expects
- Sort positions
- Binary search on the gap d in [1, last - first]
- Greedily place lockers left to right whenever the gap from the last placed one is at least d
- Search for the largest feasible d (upper bound)
- Explain why greedy placement is optimal for a fixed d
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 return the chosen sites?
- What if some sites were mandatory?
- How does this change on a circular route?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs: list of n distinct integer positions and integer m with 2 <= m <= n. Output: largest d such that m sites can be chosen with every adjacent chosen pair at least d apart....
High-Level Design
For a fixed d, place the first locker at the leftmost site and each next locker at the first site at least d past the previous one. Placing as early as possible leaves the most room for the rest (exch...