Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

For a = [3, 4, -7, 2, -2, 5, -5, 1, 6], what is the length of the longest contiguous subarray whose elements sum to 0?

  1. A. 9
  2. B. 7
  3. C. 8
  4. D. 4

Correct answer: B. 7

Keep only the FIRST index where each prefix sum appears. The longest zero-sum subarray joins the earliest and the latest equal prefix values, so the answer is the largest gap, not the number of zero-sum subarrays.

More Coding Interview Problems questions

Exams that ask this topic