Find minimum processing rate
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Find minimum processing rate states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= piles.length <= 10^4
- piles.length <= H <= 10^9
- 1 <= piles[i] <= 10^9
Examples
Input: ([3, 6, 7, 11], 8)
Expected Output: 4
Explanation: At R=4: 1+2+2+3 = 8 hours <= 8. At R=3: 1+2+3+4 = 10 > 8, so 4 is the minimum.
Input: ([30, 11, 23, 4, 20], 5)
Expected Output: 30
Explanation: H equals the number of piles (5), so each pile must be cleared in exactly one hour. The rate must be at least the largest pile, 30.
Input: ([30, 11, 23, 4, 20], 6)
Expected Output: 23
Explanation: With one extra hour the rate can drop to 23: ceil(30/23)+ceil(11/23)+ceil(23/23)+ceil(4/23)+ceil(20/23) = 2+1+1+1+1 = 6 <= 6, and 22 would need 7 hours.
Input: ([1], 1)
Expected Output: 1
Explanation: Single pile of 1 item in 1 hour needs rate 1.
Input: ([1000000000], 2)
Expected Output: 500000000
Explanation: One huge pile must be split across 2 hours: ceil(1e9 / 5e8) = 2. Rate 499999999 would need 3 hours.
Input: ([5, 5, 5, 5], 4)
Expected Output: 5
Explanation: Four equal piles in exactly four hours forces clearing each in one hour, so rate = 5.
Input: ([312884470], 312884469)
Expected Output: 2
Explanation: A near-boundary big pile with one fewer hour than items: at R=2 it takes ceil(312884470/2)=156442235 <= 312884469 hours, and R=1 takes 312884470 > 312884469. So the minimum rate is 2.
Hints
- The answer R lies in the range [1, max(piles)]: rate 1 always finishes (it just may take too many hours), and any rate above max(piles) gives the same hour count as max(piles) since each pile already takes 1 hour.
- Define f(R) = sum(ceil(p / R) for p in piles), the total hours at rate R. f is monotonically non-increasing in R: a faster rate never needs more hours. This monotonicity is exactly what makes binary search correct.
- Binary search for the smallest R with f(R) <= H. Compute ceil(p / R) without floating point as (p + R - 1) // R to avoid precision bugs at large values.
- Edge case: when H equals the number of piles, every pile must finish in exactly one hour, forcing R = max(piles).