Find minimum days to deliver all orders
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates algorithm design and combinatorial optimization skills, focusing on scheduling and load-balancing under per-center capacity constraints and frequency-based counting.
Constraints
- 1 <= N (number of delivery centers)
- 0 <= len(orderCityList)
- 1 <= orderCityList[i] <= N for every order
- A center may serve at most one order per day; a non-matching (foreign) order occupies two of that center's days.
- Orders cannot be split across centers.
Examples
Input: (3, [1, 1, 3, 1, 1])
Expected Output: 3
Explanation: City counts: city1=4, city3=1. With D=3, Center1 takes 3 home orders (3 days), Center3 takes its 1 home order (1 day), and the remaining 1 order for city1 goes to Center2 as a foreign order (2 days). Max load = 3. D=2 fails, so 3 is minimal.
Input: (1, [1, 1, 1])
Expected Output: 3
Explanation: Only one center exists, and all 3 orders are home orders for it, costing 1 day each = 3 days total. There is no other center to offload to.
Hints
- Binary search on the answer D (the number of days). The feasible region is monotone: if D days suffice, so does any D' > D, so the smallest feasible D is the answer.
- Give every center a budget of D occupied-days. A home order (center id == city) costs 1 day and is always cheaper than the 2 days a foreign order costs, so for each city greedily place up to min(count[c], D) orders on their home center first.
- After placing home orders, any leftover order for any city is 'foreign' and needs a 2-day slot somewhere. A center with h home orders has D - h days left, giving floor((D - h) / 2) foreign slots. D is feasible iff total foreign slots >= total leftover (foreign) orders.