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

What does this program print?

What does this program print?
a = [6, 3, 8, 1]
mx = [0] * len(a)
mx[-1] = a[-1]
for i in range(len(a) - 2, -1, -1):
    mx[i] = max(a[i], mx[i + 1])
print(mx)
  1. A. [6, 3, 8, 1]
  2. B. [8, 3, 8, 1]
  3. C. [8, 8, 8, 1]
  4. D. [8, 8, 1, 1]

Correct answer: C. [8, 8, 8, 1]

The loop builds suffix maxima from right to left: each entry is the larger of the element and the maximum after it, so the output is the suffix-maximum array of the input.

More Coding Interview Problems questions

Exams that ask this topic