ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def sum_min(a):
MOD = 10**9 + 7
st = []
total = 0
cur = 0
for x in a:
cnt = 1
while st and st[-1][0] >= x:
v, c = st.pop()
cnt += c
cur -= v * c
st.append((x, cnt))
cur += x * cnt
total += cur
return total % MOD
print(sum_min([3, 1, 2, 4]), sum_min([11, 81, 94, 43, 3]))- A. 17 443
- B. 17 444
- C. 16 444
- D. 18 444
Correct answer: B. 17 444
A monotonic stack tracks, for each new element, the running total of minima over all subarrays that end there. [3, 1, 2, 4] gives 17, and the second array gives 444.
More Python Programming questions
- What is printed? python print(6 & 3 == 2, 5 | 2 == 7)
- What is printed? python import heapq def medians(xs): lo, hi = [], [] out = [] for x in xs: heapq.heappush(lo, -x) heapq.heappush(hi…
- What is printed? python def sieve(n): s = bytearray([1]) (n + 1) s[0:2] = bytes(2) for i in range(2, int(n 0.5) + 1): if s[i]: s[i i::i] =…
- What is printed? python a = [1, 2, 3] b = a b += [4] c = a + [5] a = a 2 print(b, c, a)
- What is printed? python def stepstocycle(f, s): seen = {} i = 0 while s not in seen: seen[s] = i s = f[s] i += 1 return i, i - seen[s]…
- What is printed? python from collections import Counter def divpairs(a, k): c = Counter(x % k for x in a) total = 0 for r in c: s = (k…