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

Array a = [-2, 3, -4, 6, -1, 2]. How many non-empty subarrays have a sum divisible by 5? Count prefix sums by their remainder, normalised to 0..4 as in Python.

  1. A. 0
  2. B. 1
  3. C. 2
  4. D. 5

Correct answer: C. 2

Two prefix sums with the same non-negative remainder mod 5 mark a divisible subarray. In C or Java, % on a negative prefix returns a negative remainder, which splits equal classes; that version gives 0 instead of 2.

More Coding Interview Problems questions

Exams that ask this topic