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

Array a = [1, 2, 3, 1, 2, 3, 1]. How many contiguous subarrays of length at least 2 have the sum of the elements at even offsets (0, 2, 4, ... from the subarray start) equal to the sum at odd offsets?

  1. A. 6
  2. B. 4
  3. C. 3
  4. D. 13

Correct answer: B. 4

Alternating sums depend on the start parity, so the test is best done with prefix sums of signed values: a subarray satisfies the rule when two prefix values of the signed array match. Checking the total sum parity instead is a different condition.

More Coding Interview Problems questions

Exams that ask this topic