Fewest days to deliver every order from distribution centers
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's online assessment, last asked Sep 2025. It tests binary-search and greedy.
There are N distribution centers numbered 1..N, and each order names a destination city, also numbered 1..N. Every order goes to exactly one center and cannot be split. A center works on one order per day. An order for the center's own city takes 1 day; an order for any other city takes 2 days of that center's time. All centers work in parallel. Return the fewest days needed to finish every order.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
min_days(n: int, order_cities: list[int]) -> int—order_cities[i]is the destination of orderi. Returns 0 when there are no orders.
Example
min_days(3, [1, 1, 3, 1, 1]) -> 3 # center 1 takes three city-1 orders, center 3 its own order,
# center 2 takes the last city-1 order (2 days)
min_days(1, [1, 1, 1]) -> 3 # one center, three home orders
min_days(2, [2, 2, 2, 2, 2]) -> 4 # center 2 takes 4 home orders, center 1 one foreign order (2 days)
Constraints
-
1 <= n;0 <= len(order_cities); every city is in1..n. - Each center handles one order per day; a foreign order occupies two of its days.
- Avoid trying every assignment: aim for about O((n + m) log m) for
morders.
Clarifying questions
- Can a center start a foreign order on one day and finish it on the next, or must the two days be contiguous? (It does not change the answer: only a center's total load matters.)
- Are there cities with no orders, and centers that may stay idle?
- Can
order_citiesbe empty, and what should that return? - Is the input large enough that an O(n · m) check is too slow?
What a strong answer covers
- The answer is monotone in the number of days
D, so binary search onDbetween 1 and2m. - The feasibility check for a given
D: each center first takes up tomin(count[c], D)of its own orders (a home order is always cheaper); leftover orders need 2-day slots, and a center withhhome orders offers(D - h) // 2of them.Dworks iff total slots >= total leftovers. - Why the greedy "home first" step is safe: moving a home order elsewhere only costs more days.
- Count orders per city once; each check is O(n), so the total is O(n log m + m).
- Edge cases: no orders, one center, every order for one city, every city with orders.
Follow-ups
- Return the assignment of orders to centers, not just the day count.
- A foreign order costs
tdays instead of 2, or the cost depends on the distance between center and city. - Centers have different daily capacities.
Where it's asked
- Amazon · Online assessment · Last asked Sep 2025Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
