← Questions

Fewest days to deliver every order from distribution centers

0
AmazonCodingLast asked Sep 2025

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 order i. 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 in 1..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 m orders.

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_cities be 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 on D between 1 and 2m.
  • The feasibility check for a given D: each center first takes up to min(count[c], D) of its own orders (a home order is always cheaper); leftover orders need 2-day slots, and a center with h home orders offers (D - h) // 2 of them. D works 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 t days 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

binary-search · greedy

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding