Minimum Subarray Sum
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mygate reportedly used Minimum Subarray Sum in an OA in September 2023, and the detail that trips people is right in the statement: there's no target-sum parameter. You just return the smallest sum of any nonempty contiguous subarray. It's a Kadane's variant wearing a prefix-sum costume. With n up to 10^5 and values up to 10^9, brute force dies and overflow is a real risk. If you blank on the recurrence during the live OA, StealthCoder sits invisibly on your screen as a safety net. But you won't need it once you see the flip.
The problem
Given a nonempty integer array nums, return the smallest sum of any nonempty contiguous subarray. A subarray consists of consecutive elements. Return its sum, rather than its length or indices. There is no target-sum parameter. Function minimumSubarraySum(nums: int[]) → long Examples Example 1 nums = [3,-4,2,-3,-1,7,-5] return = -6 The subarray [-4, 2, -3, -1] has sum -6. Example 2 nums = [2,4,1] return = 1 The smallest nonempty sum is the single element 1. Example 3 nums = [-5] return = -5 The only nonempty subarray has sum -5. Constraints 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 The answer fits a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is Kadane's algorithm mirrored. Track the minimum sum of a subarray ending at the current index: cur = min(nums[i], cur + nums[i]). Then keep a global minimum of every cur. That's O(n) time and O(1) space. The prefix-sum view works too: the answer is the minimum over j of prefix[j] minus the maximum earlier prefix, which gives the same result. Pitfalls are concrete. Initialize from nums[0], not 0, or an all-positive array like [2,4,1] wrongly returns 0 instead of 1. The subarray must be nonempty, so don't allow an empty reset. Use 64-bit math, since sums can reach 10^14. Example 3, a single negative element, is your edge case. If the live OA freezes you, StealthCoder can hand you the clean loop, but the logic is five lines.
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 Minimum Subarray Sum 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 Mygate's OA.
Mygate 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.
Minimum Subarray Sum FAQ
What's the trick for Minimum Subarray Sum?+
Run Kadane's with min instead of max. At each index, the best subarray ending there is either the element alone or the element added to the previous best. Track the smallest value seen across all indices. One pass, constant extra space.
How hard is this one really?+
Easy to medium. If you know Kadane's, it's a two-minute flip. If you don't, you'll probably reach for O(n^2) brute force, which times out at 10^5 elements. The Mygate version is a standard pattern with no hidden twist.
Why does [2,4,1] return 1 and not 0?+
The subarray must be nonempty. With all positives, the smallest option is the single smallest element. If you initialize your running minimum to 0, you'll return 0 and fail. Start both the current and global values from nums[0].
Do I need long or 64-bit integers?+
Yes. Values reach 10^9 in magnitude across 10^5 elements, so sums can hit about 10^14. That overflows a 32-bit int. The statement says the answer fits in signed 64-bit, so use long or the equivalent in your language.
How do I prepare for this in 48 hours?+
Write Kadane's for max, then flip it to min from memory. Test on the three examples plus an all-positive array and an all-negative array. Then try the prefix-sum version with a running max of earlier prefixes. That covers every angle this problem can take.