Find minimal rate k and subset sum
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving skills, specifically search/optimization techniques for finding a minimal feasible rate and combinatorial reasoning for subset-sum problems, drawing on concepts such as binary-search-on-answer and dynamic programming.
Minimum Eating Rate (Koko Eating Bananas)
Constraints
- 1 <= len(piles) <= 10^4
- len(piles) <= h <= 10^9
- 1 <= piles[i] <= 10^9
- The answer is guaranteed to be a positive integer in [1, max(piles)].
Examples
Input: ([3, 6, 7, 11], 8)
Expected Output: 4
Explanation: At k=4: ceil(3/4)+ceil(6/4)+ceil(7/4)+ceil(11/4) = 1+2+2+3 = 8 hours <= 8. k=3 would need 9 hours, so 4 is minimal.
Input: ([30, 11, 23, 4, 20], 5)
Expected Output: 30
Explanation: With only 5 hours and 5 vaults, each vault must be finished in exactly one hour, so k must be at least the largest vault, 30.
Hints
- If a rate k works, any rate larger than k also works — so the feasibility is monotonic and you can binary search on k.
- For a given rate k, the hours needed for one vault of size p is ceil(p / k); sum these across all vaults.
- Search k in [1, max(piles)]: the lower bound is 1 (always need at least 1 unit/hour), the upper bound is max(piles) (eating the biggest vault in one hour).
- Use the 'find the leftmost feasible value' binary-search template: move hi = mid when feasible, lo = mid + 1 otherwise.
Subset Sum (does any subset reach the target?)
Constraints
- 0 <= len(nums) <= 10^3 (algorithm scales with the range of reachable sums)
- nums[i] may be any integer; with only non-negative values a 1-D boolean DP over [0, target] is the standard pseudo-polynomial approach
- target may be any integer
- The empty subset sums to 0, so target = 0 always returns True.
Examples
Input: ([2, 5, 3, 11], 10)
Expected Output: True
Explanation: The subset {2, 3, 5} sums to 10.
Input: ([2, 5, 3, 11], 4)
Expected Output: False
Explanation: No subset of {2, 5, 3, 11} sums to 4.
Hints
- Maintain the set of all sums reachable by some subset, starting from {0} (the empty subset).
- For each new number x, every previously reachable sum s yields a new reachable sum s + x — union these into the set.
- You can return early the moment `target` appears in the reachable set.
- If all values are non-negative and target is bounded, an equivalent O(n * target) boolean DP `dp[s] = True if some subset sums to s` is the textbook pseudo-polynomial solution.