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

Array a = [4, -1, 2, -3, 5, -2, 1, 1, -4]. How many DISTINCT values appear among the sums of all non-empty contiguous subarrays?

  1. A. 15
  2. B. 12
  3. C. 45
  4. D. 10

Correct answer: B. 12

Compute all prefix sums P, then every subarray sum is P[j] - P[i] for i < j. Counting distinct values, not subarrays, needs a set over the differences; counting subarrays would give the total number of intervals, which is far larger.

More Coding Interview Problems questions

Exams that ask this topic