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

Array a = [2, 4, 6, 1, 3, 5, 8, 7]. How many contiguous subarrays of length at least 2 have a sum divisible by 4?

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

Correct answer: A. 8

Count pairs of prefixes with equal remainder mod 4 whose indices differ by at least 2. Including length-1 subarrays, which the question excludes, inflates the count by the number of single elements divisible by 4.

More Coding Interview Problems questions

Exams that ask this topic