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
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
binary search on answer
greedy
sorting
maximize the minimum
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported