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?
- A. 9
- B. 7
- C. 8
- 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
- Array a = [-2, 3, -4, 6, -1, 2]. How many non-empty subarrays have a sum divisible by 5? Count prefix sums by their remainder, normalised…
- In the binary string s = "1100110100", count the non-empty substrings that contain exactly as many 0s as 1s.
- Array a = [2, 7, -3, 5, 11, 4, 9, -1, 6]. How many index pairs (i, j) with i < j have a[i] + a[j] = 10?
- Array a = [2, 1, -1, 4, -2, 3, 0, 1, 1, 0, 2]. How many indices i have the sum of the elements strictly left of i equal to the sum of the…
- Array a = [3, 1, 2, 6, 4, 5, 9]. How many index pairs (i < j) satisfy a[i] mod 3 = a[j] mod 3?
- Sorted array a = [-4, -1, 0, 2, 3, 5, 7]. How many triples of distinct indices i < j < k have a[i] + a[j] + a[k] <= 6?