Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

What does this Python program print?

What does this Python program print?
a = [2, -2, 3, -3, 1, 1]
p = {0: 1}; s = 0; ans = 0
for x in a:
    s += x
    ans += p.get(s, 0)
    p[s] = p.get(s, 0) + 1
print(ans)
  1. A. 4
  2. B. 2
  3. C. 3
  4. D. 5

Correct answer: A. 4

The map counts earlier prefix sums equal to the current one, so each hit is a zero-sum subarray. Starting the map without the empty prefix 0 drops subarrays beginning at index 0; the program prints the true count of zero-sum subarrays.

More Coding Interview Problems questions

Exams that ask this topic