Assign Packing Stations So Each Worker Is Farthest From Neighbors

Last updated: November 29, 2026

Quick Overview

A packing line has stations 0 to n-1. `arrive()` assigns the station that maximizes the distance to the nearest occupied station (lowest index on ties, station 0 if empty) and returns it; `leave(s)` frees station s.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A packing line has stations 0 to n-1. `arrive()` assigns the station that maximizes the distance to the nearest occupied station (lowest index on ties, station 0 if empty) and returns it; `leave(s)` frees station s.

An onsite design-a-structure question in Amazon's style, framed around spacing associates along a fulfillment center line. The interviewer looks at how you model gaps between occupied stations, handle the two open-ended edges, and whether you can move from an O(n) scan to a heap of gaps.

What the Interviewer Expects
  • Treats the edges (before the first and after the last worker) as special gaps
  • Computes the best spot in an interior gap as its midpoint
  • Breaks ties by lowest index
  • Implements leave by merging neighbors
  • Discusses a heap of gaps with lazy invalidation for O(log n)
Key Topics to Cover
design a data structure
sorted lists
heaps
interval gaps
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
  • Can you make arrive O(log n) instead of O(k) in the number of workers?
  • What if certain stations are broken and must be skipped?
  • How would you handle n up to 10^9?
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

Stations 0..n-1. On arrival, choose the free station maximizing the distance to the closest occupied station; ties go to the lowest index. With nobody present, choose 0. leave is only called on oc...

High-Level Design

Keep the occupied stations sorted. Candidate spots come from three kinds of gaps: the left edge (choose 0, distance taken[0]), each interior gap (a, b) (choose a + (b - a) // 2, distance `(b - a...


Submit Your Answer
Markdown supported