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

Array [3, 1, 2, 5, 0, 4, 2]. How many contiguous subarrays have a sum equal to twice their length?

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

Correct answer: D. 7

Subtract 2 from every element; the condition becomes that the shifted subarray has zero sum, which prefix sums with a hash map count in linear time. Forgetting the shift and testing equality with the length solves a different problem.

More Coding Interview Problems questions

Exams that ask this topic