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

Array a = [4, 2, 7, 1, 8, 3, 6, 5]. What is the largest sum of a contiguous subarray whose length is between 2 and 4 inclusive?

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

Correct answer: C. 22

Use prefix sums, then for each end j take P[j] minus the minimum prefix among indices j-4 to j-2, which keeps the window length in range. Ignoring the lower length bound allows single elements, while the upper bound is the constraint that limits the window.

More Coding Interview Problems questions

Exams that ask this topic