Problem
Longest Zero Sum Subarray
Learn this problemProblem statement
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[]) → intExamples
Example 1
nums = [15, -2, 2, -8, 1, 7, 10, 23]return = 5The subarray [-2, 2, -8, 1, 7] has sum 0 and length 5.
Example 2
nums = [1, 2, 3]return = 0No non-empty contiguous subarray has sum 0.
Constraints
1 <= nums.lengthnums[i]may be positive, negative, or zero.
More Amazon problems
- Find Maximum Total Amount (SDE I, Fungible :)OA · Seen Jul 2026
- Meeting Rooms IIPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · ONSITE INTERVIEW · Seen Jul 2026
- Single Element in a Sorted ArrayPHONE SCREEN · Seen Jul 2026
- Count the Number of Complete ComponentsPHONE SCREEN · Seen Jul 2026
- Cousins in Binary Tree IIONSITE INTERVIEW · Seen Jul 2026
- Minimum Operations to Make an Array ContinuousONSITE INTERVIEW · Seen Jul 2026
- Vertical Order Traversal of a Binary TreeONSITE INTERVIEW · Seen Jul 2026