Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the maximum total of two non-overlapping subarrays of length 2 in [1, 5, 2, 8, 3]? Run the code.

Problem: What is the maximum total of two non-overlapping subarrays of length 2 in [1, 5, 2, 8, 3]? Run the code.
a = [1, 5, 2, 8, 3]
win = [sum(a[i:i + 2]) for i in range(len(a) - 1)]
best = 0
for i in range(len(win)):
    for j in range(i + 2, len(win)):
        best = max(best, win[i] + win[j])
print(best)
What is printed?
  1. A. 21
  2. B. 18
  3. C. 17
  4. D. 11

Correct answer: B. 18

Window sums are 6, 7, 10, 11; non-overlapping pairs need starts at least 2 apart, best 7 + 11 = 18. Allowing overlap gives 10 + 11 = 21, requiring a gap of 3 gives 6 + 11 = 17, and 11 is one window.

More Coding Interview Problems questions

Exams that ask this topic