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

Array a = [3, -1, 2, 6, -4, 5, 1, -2]. How many non-empty contiguous subarrays have a sum divisible by 4?

  1. A. 10
  2. B. 12
  3. C. 7
  4. D. 26

Correct answer: A. 10

Prefix sums sharing the same remainder mod 4 bound a subarray with a sum divisible by 4. Count how many prefixes share each remainder, starting with the empty prefix, and add C(count, 2) over the groups in a single pass.

More Coding Interview Problems questions

Exams that ask this topic