Maximize Sum of Two Non Overlapping Fragments
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft hit you with this in December 2024, and it's a dynamic programming trap dressed as an optimization problem. You're asked to pick two non-overlapping subarrays and maximize their combined sum. Sounds simple until you realize the state space explodes. StealthCoder reads the exact constraints and feeds you the DP transition in real time, so you're not staring at a blank editor when the clock ticks down.
Pattern and pitfall
The trick is that you can't just greedily pick the two largest subarrays. They might overlap. You need to track three things as you scan left to right: the max sum ending at or before the current index (left fragment), the max sum starting at or after the current index (right fragment), and whether you've already committed to a first fragment. Build prefix and suffix arrays where prefix[i] is the max subarray sum in [0, i] and suffix[i] is the max in [i, n-1]. Then iterate through split points and compute the best left fragment before that point plus the best right fragment after it. The pattern is classic DP with preprocessing, but the implementation details trip candidates who don't see the prefix-suffix structure upfront. StealthCoder spots this immediately and hands you the state transitions so you can code without the cognitive load.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Maximize Sum of Two Non Overlapping Fragments 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximize Sum of Two Non Overlapping Fragments FAQ
Is this asking for the maximum subarray sum problem, twice?+
Not quite. Kadane's algorithm finds one max subarray. Here you need two non-overlapping ones. You can't run Kadane twice because the second call might pick a fragment that overlaps the first. You must precompute the best fragment in every prefix and suffix, then iterate split points.
What if the optimal fragments are at opposite ends of the array?+
That's a valid case. Your solution must handle it. If you only iterate through split points in the middle, you'll catch it: best left fragment is in [0, i], best right is in [i+1, n-1]. When i=0, right is [1, n-1], and when i=n-2, left is [0, n-2]. Edge cases baked in.
How do I handle negative numbers?+
A fragment can have a negative sum if the alternative is no fragment at all, but usually the problem expects non-empty subarrays. Check: can you pick an empty fragment? If not, a single negative element is still valid. Kadane returns at least one element, so your prefix and suffix arrays will include single-element subarrays with negative values.
Is there a way to do this in one pass?+
Not cleanly. You need to precompute because you don't know where the optimal split point is until you've seen the whole array. Build prefix in one pass, suffix in another, then merge in a third. Three passes is standard and expected for this pattern.
What if both fragments have the same start or end index?+
They can't. The problem says non-overlapping. If fragment 1 ends at index i, fragment 2 must start at index i+1 or later. Your split point logic enforces this: left is [0, i], right is [i+1, n-1], zero overlap.