Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: For the array [1, 3, 5, 7, 9], how many subarrays have an average equal to 5?

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

Correct answer: A. 3

A subarray has average 5 when its sum equals 5 times its length. Checking the subarray [5] and the subarrays [3, 5, 7] and [1, 3, 5, 7, 9] (the centred ones) gives the count.

More Coding Interview Problems questions

Exams that ask this topic