Skip to main content
Intermediatecoding Verified

Given an array of integers, find the length of the longest subarray whose sum is zero, then optimise your solution.

ReportedSource: GeeksforGeeks interview experience: Cognizant Software Engineer

Quick answerFree

Prefix sums with a map of first occurrence (seed {0: -1}); equal prefix sums at i and j mean a zero-sum subarray of length j - i; track the max. O(n). Sliding window fails with negatives.

Detailed answer· ~120 words

Interview Prep

Unlock the full answer with Interview Prep

Get a ~120-word model answer and a runnable reference solution, plus 61 more DSA questions.

Also in Pro (₹499 / 90 days).

Reference solution· runs in the browser sandbox

Interview Prep
Unlock with Interview Prep

+ 61 more DSA questions in Interview Prep, each with the full answer.

Discussion