Solve minimum rate 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 by combining resource-rate scheduling for finishing workloads within a deadline and combinatorial subset-sum decision-making, while requiring formal time and space complexity analysis.
Minimum Rate to Empty All Vaults
Constraints
- 1 <= vaults.length <= 10^5
- 1 <= vaults[i] <= 10^9
- vaults.length <= h <= 10^9 (h is at least the number of vaults, so a feasible rate always exists)
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 <= 8. At k=3 it would be 1+2+3+4 = 10 > 8, so 4 is minimal.
Input: ([30, 11, 23, 4, 20], 5)
Expected Output: 30
Explanation: With only 5 hours for 5 vaults, each vault must finish in exactly 1 hour, so k must be at least the largest vault (30).
Hints
- The number of hours required is a monotonic function of the rate k: a larger k never increases the total hours. This monotonicity is exactly what enables binary search.
- Search the integer rate over the range [1, max(vaults)]. For a candidate k, the total hours are sum(ceil(v/k)) over all vaults. Use ceil(v/k) = (v + k - 1) // k to avoid floating point.
- Find the smallest k for which total hours <= h. The lower bound is 1 (at least 1 unit/hour) and the upper bound is max(vaults) (one vault per hour is always feasible since h >= number of vaults).
Subset Equals Target Sum
Constraints
- 0 <= nums.length <= 200
- 1 <= nums[i] <= 1000 (non-negative integers)
- 0 <= target <= 200000
- The empty subset is allowed, so target = 0 is always achievable.
Examples
Input: ([2, 5, 3, 11], 10)
Expected Output: True
Explanation: 2 + 5 + 3 = 10, so a valid subset exists.
Input: ([2, 5, 3, 11], 4)
Expected Output: False
Explanation: No subset of {2,5,3,11} sums to 4 (2+anything overshoots or undershoots; 3 alone is 3, 2 alone is 2).
Hints
- Frame it as a reachability problem: track the set of sums you can form using a prefix of the elements. Start with {0} (the empty subset).
- For each new number n, every previously reachable sum s gives a new reachable sum s + n. Discard sums that exceed target, since the values are non-negative and can never come back down.
- The standard formulation is a boolean DP array dp[0..target] where dp[j] means sum j is reachable; iterate j downward when updating in place to ensure each element is used at most once. Return dp[target].