Reported July 2026
Googleprefix sum

Longest Subarray with Sum at Most K

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

Google's July 2026 report of this OA looks like a sliding window problem and isn't. Strip the wording and it's a prefix sum question: find the farthest pair of indices where the sum difference stays at or below k. With negatives allowed, the window trick breaks, and that's the whole point of the question. If you're taking this in the next day or two, learn the reduction now. StealthCoder sits behind the live OA as a safety net if your mind goes blank on the monotonic structure, but the idea is small enough to own before you start.

The problem

Given an integer array nums and an integer k, return the maximum length of a non-empty contiguous subarray whose sum is at most k.
The array may contain positive, zero, and negative values, so a standard positive-only sliding window is not sufficient.
Return 0 when no non-empty subarray satisfies the limit.

Function
longestSubarrayAtMostK(nums: int[], k: long) → int

Examples
Example 1
nums = [1,2,-1,2]
k = 3
return = 3
The subarray [1, 2, -1] has sum 2 and length 3. The full array has sum 4.
Example 2
nums = [5,-10,5]
k = 0
return = 3
The entire array sums to 0; the negative value makes the longest valid window non-monotonic.

Constraints
1 <= nums.length <= 200000
-10^9 <= nums[i] <= 10^9
-10^14 <= k <= 10^14
Subarray sums fit in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build prefix sums P[0..n]. You want the max j - i with i < j and P[j] - P[i] <= k, which means P[i] >= P[j] - k. Because negatives exist, a two-pointer window can't shrink safely. The clean approach: keep a stack of candidate left indices where prefix values strictly decrease, since a later index with a larger or equal prefix is never a better left endpoint than an earlier smaller one. Wait, you need the largest P values for the condition, so build the stack with indices whose prefix is a new maximum going left to right, then binary search for the leftmost i with P[i] >= P[j] - k. Store prefix maxima as a suffix-max array and binary search on it, O(n log n). Pitfalls: use 64-bit sums, require non-empty (j > i), and return 0 if nothing fits. StealthCoder is the hedge if you freeze mid-OA.

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 Longest Subarray with Sum at Most K 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

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

Longest Subarray with Sum at Most K FAQ

What's the trick in Longest Subarray with Sum at Most K?+

Convert to prefix sums and search for the farthest left index whose prefix is at least P[j] - k. Negatives kill the normal sliding window, so you need a monotonic structure over prefixes plus binary search, giving O(n log n).

Why doesn't a plain sliding window work here?+

Sliding windows assume that extending the window only increases the sum. With negative values, a longer window can have a smaller sum, so validity isn't monotonic. Shrinking from the left can discard the best answer, which Example 2 shows.

How hard is this one really for Google?+

Medium-hard. The code is short, but you have to spot the prefix sum reduction and the monotonic candidate idea. If you've seen shortest subarray with sum at least K, it feels familiar. Cold, it's easy to burn twenty minutes on windows.

What edge cases should I test before submitting?+

Test all negatives, all positives bigger than k, a single element, k negative, and values near 10^9 with length 200000. Use 64-bit integers for prefixes. Confirm you return 0 when no non-empty subarray fits, and never count an empty one.

How do I prepare in 48 hours for this pattern?+

Write prefix sums plus binary search from scratch twice. Then do one problem with a monotonic deque over prefix sums. Focus on why discarding dominated indices is safe. Skip broad grinding, this reduction is the one idea you need.

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