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

Array a = [7, -3, 2, 5, -8, 4, 1, -1]. How many contiguous subarrays have a sum divisible by 3? (Zero is divisible by 3.)

  1. A. 12
  2. B. 9
  3. C. 14
  4. D. 10

Correct answer: D. 10

Prefix sums with the same remainder mod 3 give a divisible subarray between them. Remainders of negative values must be normalised before counting, and zero counts as divisible, which the empty-prefix seed takes care of.

More Coding Interview Problems questions

Exams that ask this topic