Reported September 2026
Oracleprefix sum

Maximum Subarray Sum with Length at Most K

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

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

The edge case that kills the naive solution on this Oracle OA, reported in September 2026, is the all-negative array. Plenty of people init their best to 0 and return a sum for an empty subarray, which the problem explicitly forbids. The task is the max sum of a non-empty subarray with length at most k, and it's a prefix-sum problem with a sliding window minimum on top. Brute force is O(n*k) and dies at 2 * 10^5. If you blank on the deque part, StealthCoder is the safety net running invisibly during the live assessment.

The problem

Given an integer array nums and an integer k, return the maximum sum of a non-empty contiguous subarray whose length is at most k.
The array may contain negative values, so the answer may be negative.

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

Examples
Example 1
nums = [-2,3,-1,5,-6]
k = 3
return = 7
The length-three subarray [3, -1, 5] has sum 7, which is maximal.
Example 2
nums = [-5,-2,-7]
k = 2
return = -2
The subarray must be non-empty, so the best choice is the single value -2.

Constraints
1 <= nums.length <= 2 * 10^5
1 <= k <= nums.length
-10^9 <= nums[i] <= 10^9
The result fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build prefix sums P where P[0] = 0. The sum of a subarray ending at i with length at most k is P[i] - P[j], where j is in [i-k, i-1]. So for each i you want the minimum P[j] in that window. Keep a monotonic deque of indices with increasing prefix values, pop from the front when the index falls out of range, and pop from the back while the new value is smaller. Compute the candidate before pushing index i. The pitfalls: initialize best to negative infinity (Long.MIN_VALUE), not 0, or Example 2 returns 0 instead of -2. Use 64-bit for prefix sums, since values reach 2 * 10^14. Off-by-one on the window is the other classic bug. If the deque logic slips under pressure, StealthCoder can hand you a clean version live. Total time is O(n).

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Maximum Subarray Sum with Length 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Oracle reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Subarray Sum with Length at Most K FAQ

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

Turn it into prefix sums. For each end index i, you need the smallest prefix value among the last k positions. A monotonic deque gives you that minimum in amortized O(1), so the whole thing runs in O(n).

Why does my solution fail the all-negative case?+

You probably start the best answer at 0. The subarray has to be non-empty, so with [-5,-2,-7] and k=2 the answer is -2, not 0. Start best at the smallest possible long and only update it with real candidates.

Can I just use Kadane's algorithm here?+

Not directly. Kadane doesn't cap the subarray length, so it can return a window longer than k. You need the window-limited minimum prefix, which is why the deque or a similar structure is required.

Do I need 64-bit integers?+

Yes. Each value reaches 10^9 in magnitude and the array has up to 2 * 10^5 elements, so sums go past 32-bit range. Use long for prefix sums and the answer, and watch for overflow in any sentinel you pick.

How do I prepare for this in 48 hours?+

Practice two things: prefix sums with window bounds, and the monotonic deque for sliding window minimum. Write the deque version from scratch twice, then test it on all-negative input, k=1, and k=n. Those three cases catch most bugs on this problem.

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

OA at Oracle?
Invisible during screen share
Get it