ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def nge_circ(a):
n = len(a)
res = [-1] * n
st = []
for i in range(2 * n):
x = a[i % n]
while st and a[st[-1]] < x:
res[st.pop()] = x
if i < n:
st.append(i)
return res
print(nge_circ([1, 2, 1]))- A. [2, -1, -1]
- B. [2, 2, -1]
- C. [2, -1, 2]
- D. [-1, 2, -1]
Correct answer: C. [2, -1, 2]
Looping twice over the array lets the last 1 find the 2 that wraps around. The first pass pushes indices, and the second pass only resolves the ones still waiting.
More Python Programming questions
- What is printed? python def dsum(n): return n if n < 10 else dsum(sum(map(int, str(n)))) print(dsum(9875), dsum(0), dsum(999999999999))
- What is printed? python def lcp(words): out = [] for col in zip(words): if len(set(col)) == 1: out.append(col[0]) else: break return…
- What is printed? python s = 'abc' 2 + 'd' print(s[::3], 'bcd' in s, s.index('c', 3))
- What is printed? python print(range(0, 10, 3) == range(0, 11, 3), range(0, 10, 3)[-1], len(range(1018)))
- What is printed? python s = 'abc' t = s s += 'd' u = list('abc') v = u u += ['d'] print(s, t, u is v, v)
- What is printed? python print(bin(0b1011011).count('1'), (0b1011011).bitcount(), 0b1011011 & (0b1011011 - 1))