Partition Into K Subarrays With Sum at Least X
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's June 2026 OA reports include a partition problem where every piece must sum to at least x, and Example 3 hides the whole catch: nums = [10,-5,5] with k = 2 and x = 5. Cut right after the 10 and you fail. Cut after the -5 and you pass. That negative number kills the easy greedy you'd reach for first. It's a dynamic programming problem over prefix sums, with n capped at 2000, so an O(n*k) solution is the target. If you freeze on the transition during the live OA, StealthCoder runs invisibly as a safety net and hands you the structure.
The problem
You are given an integer array nums and integers k and x. Determine whether the entire array can be partitioned, in its original order, into exactly k non-empty contiguous subarrays such that the sum of every subarray is at least x. Return true if such a partition exists. Otherwise, return false. Implement canPartitionWithMinimumSum(nums, k, x). Function canPartitionWithMinimumSum(nums: int[], k: int, x: int) → boolean Examples Example 1 nums = [3,1,4,2,2] k = 3 x = 4 return = true One valid partition is [3,1] | [4] | [2,2]. All three subarray sums equal 4. Example 2 nums = [100,1,1] k = 2 x = 50 return = false The only cut positions produce sums 100 and 2, or 101 and 1. In both cases, one subarray sum is below 50. Example 3 nums = [10,-5,5] k = 2 x = 5 return = true The partition [10,-5] | [5] has subarray sums 5 and 5. Cutting immediately after 10 would fail, which is why a positive-only greedy rule does not handle negative values. Constraints 1 <= nums.length <= 2000. 1 <= k <= nums.length. -10^9 <= nums[i] <= 10^9. 1 <= x <= 10^9. Subarray sums may exceed 32-bit integer range.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build prefix sums P. Define dp[j][i] as true if the first i elements split into exactly j valid subarrays. The transition is dp[j][i] is true if some m < i has dp[j-1][m] true and P[i] - P[m] >= x. Naive is O(n^2 * k), too slow at 2000. The trick is that the condition rearranges to P[m] <= P[i] - x. So for each layer, keep the minimum P[m] among all m where dp[j-1][m] is true, and only extend m as i grows. Because negatives are allowed, you can't just take the latest valid cut, which is why greedy fails. Track the minimum, not the most recent. Use 64-bit sums, since values reach 2000 * 10^9. The pitfall is forgetting that the last piece must end exactly at n. If the pattern slips away mid-assessment, StealthCoder is the hedge that keeps you moving.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Partition Into K Subarrays With Sum at Least X cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Partition Into K Subarrays With Sum at Least X FAQ
What's the trick in this Google OA problem?+
Rewrite the condition P[i] - P[m] >= x as P[m] <= P[i] - x. Then for each count j, you only need the minimum prefix sum among valid earlier cut points. That drops the transition from O(n) to O(1) and the whole solution to O(n*k).
Why doesn't a greedy approach work here?+
Greedy cuts as soon as the running sum reaches x. With negatives, that can strand a bad remainder. In [10,-5,5] with k=2 and x=5, cutting after 10 leaves [-5,5] with sum 0. Delaying the cut makes it work.
Do I need 64-bit integers?+
Yes. Values go up to 10^9 in magnitude and the array has up to 2000 elements, so prefix sums can reach 2*10^12. In Java or C++ use long. Python handles it automatically, but watch for overflow in other languages.
How hard is this really?+
Medium-hard. The DP definition is standard, but spotting the min-prefix optimization is what separates passing from timing out. n = 2000 means O(n^2 * k) will likely be too slow, so you need the optimized transition.
How do I prepare in 48 hours?+
Write the O(n^2 * k) DP first so you trust the recurrence. Then optimize it by tracking the minimum valid prefix per layer. Test on the three examples, especially the negative one. Also check edge cases: k = 1 and k = n.