FastPrepZero-Sum Subarray: Exists and Longest
Problems/Zero-Sum Subarray: Exists and Longest

Zero-Sum Subarray: Exists and Longest

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The task: find the longest stretch of an array that adds up to zero. This also tells you whether one exists.

The report is a February 2022 post by an engineer with four years of experience, interviewing for a CS role in Bengaluru. The director round had happened, and its feedback was still pending when the post went up. In the first round, a coding round, the longest version came as a follow-up to an earlier part that only asked whether such a stretch exists. The post wants a contiguous stretch in both parts. It gives no limits, examples, output form or result.

We chose these rules: both parts are one problem, answered by a single length, where 0 means no such stretch. The stretch must not be empty. There are 1 to 10^4 values, each from -10^4 to 10^4. The examples are new.

Our version is about 85% the same as the reported question.

Given an integer array nums, return the length of the longest non-empty contiguous subarray whose values add up to 0. If no such subarray exists, return 0.

So one answer covers two questions: the answer is greater than 0 exactly when some zero-sum subarray exists, and its value is the longest length.

Function

longestZeroSumSubarray(nums: int[]) → int

Examples

Example 1

nums = [4,-1,-3,2,5,-7]return = 6

[4,-1,-3] adds up to 0 (length 3), and so does [2,5,-7] (length 3). The whole array also adds up to 0: 4 - 1 - 3 + 2 + 5 - 7 = 0. So the longest has length 6.

Example 2

nums = [3,1,2]return = 0

All values are positive, so no stretch can add up to 0. No zero-sum subarray exists, and the answer is 0.

Example 3

nums = [5,0,6]return = 1

The single 0 is a subarray of length 1 with sum 0. Nothing longer adds to 0, so the answer is 1.

Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4

More Adobe Phone Screen questions

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
  • Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
  • Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
  • Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
  • Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
  • Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
  • Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026

Similar questions at other companies

  • Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
  • Equal Sum Split After One RemovalGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Maximum Size Subarray Sum Equals KeBay · PHONE SCREENeBay interview questions
  • Detect a Contiguous Subarray with Target SumMeta · PHONE SCREENMeta interview questions
  • Count Subarrays With Target SumAmazon · PHONE SCREEN
  • Count Subarrays with Sum KByteDance · PHONE SCREENByteDance interview questions
  • Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Longest Equal-Zero-One SubarrayMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
  • Shortest Subarray with an Exact Target SumPeopleStrong · PHONE SCREENPeopleStrong interview questions
  • Find a Target-Sum SubarrayMorgan Stanley · PHONE SCREENMorgan Stanley interview questions

Interview reports that mention this question

  • Adobe CS1 Interviews: 14 Experiences (Part 4 of 6)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
  • Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
All Adobe interview questionsSee Adobe hiring insights
Saved
class Solution {
    public int longestZeroSumSubarray(int[] nums) {
        // Write your code here.
    }
}
Checking account…