Reported July 2026
Safe Securityprefix sum

Maximum Subarray Sum

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

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

Brute force checks every subarray, and that's O(n^2) at best once the array gets long. Safe Security reported this Maximum Subarray Sum question in July 2026, and the input size is the whole point. You need a single pass. The hinted pattern is prefix-sum, and it maps cleanly to the running-sum idea behind Kadane's algorithm. If you've seen it, you'll finish fast. If you blank on the trick, StealthCoder is the invisible safety net running on your desktop during the live OA, reading the problem and handing you a solution. Either way, know the one-pass idea before you open the assessment.

The problem

You are given an integer array nums. Find the contiguous subarray with the largest sum and return that sum.

Function
maxSubArray(nums: int[]) → int

Examples
Example 1
nums = [-2,1,-3,4,-1,2,1,-5,4]
return = 6
The best contiguous subarray is [4,-1,2,1], whose sum is 6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is Kadane's algorithm. Walk the array once and track the best sum of a subarray ending at the current index. At each element, either extend the previous run or start fresh: cur = max(x, cur + x). Keep a separate best that records the max cur seen. This is the prefix-sum idea in disguise, since the best subarray ending at i is prefix[i] minus the smallest earlier prefix. The classic pitfall is initializing best to 0. That breaks when every number is negative, because the answer is then the largest single element, like -1. Start both cur and best at nums[0]. Also watch for empty-input assumptions. It's O(n) time and O(1) space. If the recurrence slips your mind mid-assessment, StealthCoder can surface it while you stay in control of what you submit.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

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. If you're reading this with an OA window open, you're who this was built for.

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 Safe Security's OA.

Safe Security reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Subarray Sum FAQ

What's the trick to Maximum Subarray Sum?+

Kadane's algorithm. Keep a running sum ending at the current element. If the running sum goes below the current number, restart from that number. Track the maximum seen across the pass. One loop, constant extra space, no nested iteration needed.

How hard is this really for the Safe Security OA?+

It's an easy-to-medium classic. The logic is short, but people lose points on edge cases. All-negative arrays and single-element arrays are the usual traps. If you know Kadane's, it's a five-minute problem.

Why does initializing the best sum to 0 fail?+

If every element is negative, the correct answer is the largest negative number, not 0. Starting best at 0 implies an empty subarray, which the problem doesn't allow. Initialize with nums[0] and loop from index 1.

How does prefix sum relate to this problem?+

The sum of a subarray ending at i equals prefix[i] minus an earlier prefix. To maximize it, subtract the smallest earlier prefix. Track that minimum as you go. It gives the same answer as Kadane's in one pass.

How do I prepare for this in 48 hours?+

Write Kadane's from memory twice. Then test it on [-2,1,-3,4,-1,2,1,-5,4], which should return 6, plus [-1], [-3,-1,-2], and [5]. Learn the recurrence, not just the code, so you can adapt if the prompt changes slightly.

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

OA at Safe Security?
Invisible during screen share
Get it