Data Structures & AlgorithmsModerateTCS NQT · sliding window

Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code.

Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code.
a = [1, -2, 3, 4, -1]
print(max(sum(a[i:i + 3]) for i in range(len(a) - 2)))
What is printed?
  1. A. 5
  2. B. 6
  3. C. 2
  4. D. 7

Correct answer: B. 6

The windows sum to 2, 5 and 6 (3 + 4 - 1), so the maximum is 6.

More Coding Interview Problems questions

Exams that ask this topic