Reported February 2025
Googledynamic programming

Maximum Subarray

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The return type is a signed 64-bit long, and that one detail tells you what Google wants you to notice. Google reported this Maximum Subarray OA in February 2025, with up to 200000 elements and values as large as 10^9 in magnitude. Sums can blow past 32-bit range fast. The pattern is a classic dynamic programming scan, usually called Kadane's algorithm. It's a short problem, but the edge cases decide whether you pass. If you blank on the recurrence during the live OA, StealthCoder runs invisibly as a safety net and puts the solution on screen.

The problem

Given a non-empty integer array nums, return the maximum possible sum of a non-empty contiguous subarray.
The result is a signed 64-bit integer.

Function
maxSubarraySum(nums: int[]) → long

Examples
Example 1
nums = [-2,1,-3,4,-1,2,1,-5,4]
return = 6
The contiguous subarray [4, -1, 2, 1] has the maximum sum, 6.
Example 2
nums = [-8,-3,-6]
return = -3
A valid subarray must be non-empty, so the best choice is the single value -3.

Constraints
1 <= nums.length <= 200000
-10^9 <= nums[i] <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is one pass with two variables. Track the best sum ending at the current index, then the best seen overall. At each element, the running sum is max(x, running + x). That means you drop the old prefix the moment it goes negative. Update the global best after every step. The common pitfall is initializing the best to 0. Example 2, [-8,-3,-6], must return -3, so start both variables at nums[0] and loop from index 1. The second pitfall is overflow. Use a 64-bit type for the running sum, since 200000 values of 10^9 reach 2*10^14. O(n) time, O(1) space. Don't reach for brute force or prefix-sum pairs, they're slower and add nothing here. If the recurrence slips away mid-assessment, StealthCoder is the hedge that gives you the working code without the panic.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Maximum Subarray 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum subarray. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google 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.

Maximum Subarray FAQ

How hard is Maximum Subarray really in the Google OA?+

It's easy if you know Kadane's algorithm and awkward if you don't. The code is about five lines. The difficulty is seeing that a negative running sum should be discarded. The all-negative case in Example 2 is where most people lose points.

What's the trick to solve it in O(n)?+

Keep a running sum ending at the current element. At each step, set it to max(current value, running sum + current value). Track the largest value that running sum ever reaches. One loop, two variables, no extra array needed.

Why does the problem mention a signed 64-bit result?+

With 200000 elements up to 10^9 each, the total can reach about 2*10^14, which overflows a 32-bit int. Use long, long long, or int64 for the running and best sums. In Python it's not an issue, but in Java or C++ it is.

How do I handle arrays where every number is negative?+

Initialize both the running sum and the best answer to nums[0], then start the loop at index 1. Don't start the best at 0, because the subarray must be non-empty. For [-8,-3,-6] the correct answer is -3, the largest single element.

How do I prepare for this in 48 hours?+

Write Kadane's from memory three times, then test it on the two given examples plus a single-element array and an all-negative array. Then try a variant that also returns the start and end indices. That covers most follow-ups an OA might add to this problem.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it