Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement

How many contiguous subarrays of [1, 3, 8, 1, 3, 6, 0, 0, 9] have a sum divisible by 3? (Count equal prefix-sum remainders.)

  1. A. 6
  2. B. 28
  3. C. 45
  4. D. 29

Correct answer: D. 29

Two prefixes with the same remainder bound a subarray whose sum is divisible by 3, so add C(f, 2) over remainder classes, including the empty prefix 0: 29. Forgetting that seed gives 28, counting only single elements gives 6 and 45 is every subarray.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic