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))
- A. [3, 4, 5, 9] 4
- B. [3, 4, 5, 9, 6] 5
- C. [3, 1, 4, 5, 9] 5
- 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
- For an array of 10^6 numbers, which approach finds the largest sum of k consecutive elements fastest and correctly?
- Which statement about the bisect module is FALSE?
- What is printed? python def mink(piles, h): lo, hi = 1, max(piles) while lo < hi: mid = (lo + hi) // 2 if sum(-(-p // mid) for p in piles)…
- What is printed? python import heapq def huff(freqs): h = list(freqs) heapq.heapify(h) cost = 0 while len(h) > 1: a = heapq.heappop(h) b =…
- What is printed? python def zeros(n): c = 0 while n: n //= 5 c += n return c print(zeros(100), zeros(25))
- What is printed? python def mincut(s): n = len(s) pal = [[False] n for in range(n)] for i in range(n - 1, -1, -1): for j in range(i, n)…