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

Array [5, -5, 2, 3, -2, 7, 3]. How many contiguous subarrays have a sum that is 0, 5 or 10?

  1. A. 11
  2. B. 10
  3. C. 8
  4. D. 5

Correct answer: B. 10

Of the 28 subarray sums, 2 equal 0, 5 equal 5 and 3 equal 10: 10 in total (a prefix-sum hash map counts each target in O(n)). Counting every multiple of 5 also catches the sum −5 (11), forgetting the zero-sum subarrays gives 8, and counting only sum 5 gives 5.

More Coding Interview Problems questions

Exams that ask this topic