Data Structures & AlgorithmsModerateTCS NQT · Python basics

Problem: What does the following print? It takes the maximum of every window of size 2 in [4, 2, 12, 11, -5].

Problem: What does the following print? It takes the maximum of every window of size 2 in [4, 2, 12, 11, -5].
a = [4, 2, 12, 11, -5]
print([max(a[i:i + 2]) for i in range(len(a) - 1)])
What is printed?
  1. A. [4, 12, 12, 11]
  2. B. [4, 12, 11, -5]
  3. C. [2, 12, 11, -5]
  4. D. [4, 12, 12, 11, -5]

Correct answer: A. [4, 12, 12, 11]

Four windows of size 2 exist, so the output has four values. The windows are [4,2], [2,12], [12,11] and [11,-5].

More Coding Interview Problems questions

Exams that ask this topic