Maximum Subarray Sum
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mygate's Maximum Subarray Sum, reported in September 2026, looks like a fancy array problem but it's really one running number and one max. You're picking the best contiguous slice, and the sum can go negative, so the empty subarray trick doesn't save you. If you've seen Kadane's algorithm, this takes ten minutes. If you haven't, it's still short once you see the shape. And if your mind goes blank when the timer starts, StealthCoder runs invisibly during the live OA and gives you the solution as a safety net.
The problem
Given a nonempty integer array nums, return the largest sum of any nonempty contiguous subarray. A subarray consists of consecutive elements. Values may be negative. Return the sum, rather than the subarray indices. Function maximumSubarraySum(nums: int[]) → long Examples Example 1 nums = [-2,1,-3,4,-1,2,1,-5,4] return = 6 The subarray [4, -1, 2, 1] has sum 6, the maximum. Example 2 nums = [-8,-3,-6] return = -3 The nonempty subarray [-3] is optimal; an empty subarray is not allowed. Example 3 nums = [5] return = 5 The only nonempty subarray contains 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 problem reduces to one decision per element: extend the current subarray or start fresh at this element. Keep cur = max(x, cur + x) and best = max(best, cur). That's Kadane, O(n) time, O(1) space. The prefix-sum view works too: best sum ending at i is prefix[i] minus the minimum earlier prefix. The classic pitfall is initializing best to 0. Example 2 has all negatives and expects -3, so start best and cur from nums[0], not zero. Second pitfall: overflow. With 10^5 values up to 10^9, sums reach 10^14, so use a 64-bit type, since the function returns long. Third, don't return indices. Only the sum is asked. If you blank on the recurrence during the Mygate OA, StealthCoder is the hedge that reads the problem and hands you the working code without the proctor seeing it.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Maximum 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum subarray. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Mygate's OA.
Mygate reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Subarray Sum FAQ
How hard is the Mygate Maximum Subarray Sum question really?+
Easy to medium. It's a well-known pattern with a short solution. The difficulty is the edge cases: all-negative arrays and 64-bit overflow. If you handle those two, the core logic is about five lines.
What's the trick to solving it?+
Kadane's algorithm. At each element, choose the larger of the element alone or the element added to the running sum. Track the best value seen so far. One pass, constant memory, no nested loops needed.
Why does initializing the answer to 0 fail?+
Because the subarray must be nonempty. For [-8,-3,-6] the answer is -3, not 0. Initialize both the running sum and the best to nums[0], then loop from index 1.
Do I need a 64-bit integer here?+
Yes. Length goes up to 10^5 and values up to 10^9 in magnitude, so sums can reach about 10^14. A 32-bit int overflows. The function signature returns long for this reason, so use long or the equivalent.
How do I prepare for this in 48 hours?+
Write Kadane from memory twice, then test it on the three examples plus an all-negative case and a single element. Also learn the prefix-sum version as a backup. That's enough for this problem and its common variants.