Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

Array [2, 7, 4, 9, 5, 8]. What is the largest sum of a contiguous subarray having at most 3 elements?

  1. A. 26
  2. B. 19
  3. C. 35
  4. D. 22

Correct answer: D. 22

Window sums of length 1, 2 and 3 cover all candidates; the best is a window of three large values. Allowing longer windows would include the total sum, which is the wrong upper bound.

More Coding Interview Problems questions

Exams that ask this topic