Coding questions · DoorDash
Find minimum processing rate
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.
You are given an array piles where piles[i] is the number of items in the i-th pile and an integer H (total hours). Find the minimum integer rate R such that processing R items per hour lets you finish all piles within H hours. Assume you process one pile at a time and round up hours per pile. Describe the algorithm, prove correctness, and give time/space complexity.
What a Strong Answer Covers
- A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
- A correctness argument that explains why the algorithm covers all required cases.
- Time and space complexity, plus at least one alternative approach when relevant.
- Focused tests for normal cases, edge cases, and failure modes.
Follow-up Questions
- How would the approach change if the input were streaming or too large for memory?
- What invariants would you assert in production code?
- Which tests would catch off-by-one, duplicate, or tie-breaking bugs?
Find Minimum Processing Rate
You are given an integer array piles where piles[i] is the number of items in the i-th pile, and an integer H representing the total hours available.
A worker processes items at a fixed integer rate R items per hour. Each hour the worker picks a single pile and processes up to R items from it. If a pile has fewer than R items remaining, the worker finishes that pile this hour and does NOT move on to another pile in the same hour (one pile per hour). Equivalently, a pile of size p takes ceil(p / R) hours.
Return the minimum integer rate R such that all piles can be finished within H hours.
It is guaranteed that H >= piles.length, so an answer always exists.
Example:
- piles = [3, 6, 7, 11], H = 8 -> 4. At R=4: ceil(3/4)+ceil(6/4)+ceil(7/4)+ceil(11/4) = 1+2+2+3 = 8 <= 8. R=3 needs 1+2+3+4 = 10 > 8.
This is the classic 'Koko Eating Bananas' binary-search-on-the-answer problem.
Constraints
- 1 <= piles.length <= 10^4
- piles.length <= H <= 10^9
- 1 <= piles[i] <= 10^9
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).
Solution
Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
Problem Restatement
You are given an array piles where piles[i] is the number of items in the i-th pile and an integer H (total hours). Find the minimum integer rate R such that processing R items per hour lets you finish all piles within H hours. Assume you process one pile at a time and round up hours per pile. Describe the algorithm, prove correctness, and give time/space complexity.
Recommended Approach
Start with a brute-force baseline to confirm correctness, then identify the repeated work or ordering property that enables a better data structure such as a hash map, heap, stack, queue, two pointers, prefix sums, BFS/DFS, or dynamic programming. Write the implementation around a small invariant and test that invariant directly.
Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
Complexity
State the baseline complexity and the optimized complexity. For most interview constraints, justify why the optimized approach meets the expected input size.
Edge Cases and Tests
Empty and singleton inputs, duplicates, ties, invalid inputs, boundary values, and tests that exercise the main invariant.