ProgrammingHardAmazon OA · Flipkart coding round

What is printed?

What is printed?
from itertools import accumulate
h = [3, 1, 4, 1, 5, 9, 2, 6]
vis = [x for x, m in zip(h, [0] + list(accumulate(h, max))) if x > m]
print(vis, len(vis))
  1. A. [3, 4, 5, 9] 4
  2. B. [3, 4, 5, 9, 6] 5
  3. C. [3, 1, 4, 5, 9] 5
  4. D. [3, 4, 5, 9] 3

Correct answer: A. [3, 4, 5, 9] 4

A building is visible when it is taller than every building before it, which the running maximum checks. The heights 3, 4, 5 and 9 each beat the running maximum, giving 4 buildings.

More Python Programming questions

Exams that ask this topic