Data Structures & AlgorithmsModerateTCS NQT · Infosys

How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum to exactly 15?

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

Correct answer: B. 3

Count pairs of prefix sums that differ by 15 with a hash map of prefix values. The consecutive runs 7+8, 4+5+6, 1+2+3+4+5 and others each give one subarray; the total is the number of such runs.

More Coding Interview Problems questions

Exams that ask this topic