Longest Zero Sum Subarray
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in April 2026, and the trap is the obvious answer. Check every subarray and you're at O(n^2), which falls apart the moment the array gets big. The constraints only say length is at least 1, so assume large input. The real pattern is prefix sums plus a hash map, not dynamic programming, despite the hint. If you've seen it once, it's a five-minute problem. If you blank, StealthCoder runs invisibly during the live OA and can hand you the solution. Know the trick first, though.
The problem
You are given an integer array nums containing positive and negative integers. Return the length of the longest contiguous subarray whose sum is equal to 0. If no such subarray exists, return 0. Function longestZeroSumSubarray(nums: int[]) → int Examples Example 1 nums = [15, -2, 2, -8, 1, 7, 10, 23] return = 5 The subarray [-2, 2, -8, 1, 7] has sum 0 and length 5. Example 2 nums = [1, 2, 3] return = 0 No non-empty contiguous subarray has sum 0. Constraints 1 <= nums.length nums[i] may be positive, negative, or zero.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a running prefix sum as you scan. If the same prefix sum shows up at two indices, everything between them sums to 0. So store the first index where each prefix sum appeared in a hash map, and at each position compute the current index minus that first index. Keep the max. Seed the map with {0: -1} so subarrays starting at index 0 count. The classic pitfall is overwriting the stored index on a repeat, which shrinks your answer. Only insert when the sum is new. Also watch zeros: a single 0 gives length 1, and the seed handles a prefix that returns to 0. Time is O(n), space is O(n). If your mind goes blank mid-assessment, StealthCoder is the safety net that reads the problem on screen and gives you this approach without the proctor seeing anything.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Longest Zero Sum 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon 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 Zero Sum Subarray FAQ
What's the trick for Longest Zero Sum Subarray?+
Prefix sums with a hash map. If two indices share the same prefix sum, the subarray between them sums to 0. Store the first occurrence of each sum and maximize the index gap. Seed the map with sum 0 at index -1.
Is this really dynamic programming?+
Not in practice. The hinted label says dynamic programming, but the working solution is a running prefix sum and a hash map in one pass. You don't need a DP table. Treat it as prefix-sum plus hashing.
Why does brute force fail here?+
Checking every start and end pair is O(n^2), or O(n^3) if you re-sum each time. The constraints give no upper bound on length, so assume large input. The one-pass hash map approach runs in O(n) and avoids the timeout risk.
What edge cases should I test?+
Test an array with no zero-sum subarray, which returns 0. Test a single 0, which returns 1. Test a prefix that sums to 0 from index 0, which needs the {0: -1} seed. Test repeated sums, where you must keep the earliest index.
How do I prep for this in 48 hours?+
Write the prefix-sum hash map solution from memory twice. Then do two variants, like subarray sum equals K and the longest subarray with a given sum. They share the same skeleton, so you'll recognize the pattern fast in the Amazon OA.