ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from collections import deque
def win_max(a, k):
dq = deque()
out = []
for i, x in enumerate(a):
while dq and a[dq[-1]] <= x:
dq.pop()
dq.append(i)
if dq[0] <= i - k:
dq.popleft()
if i >= k - 1:
out.append(a[dq[0]])
return out
print(win_max([1, 3, -1, -3, 5, 3, 6, 7], 3))- A. [3, 3, 5, 3, 6, 7]
- B. [1, 3, 3, 5, 6, 7]
- C. [3, 3, 5, 5, 5, 7]
- D. [3, 3, 5, 5, 6, 7]
Correct answer: D. [3, 3, 5, 5, 6, 7]
The deque keeps indices whose values decrease from front to back, and its front is the maximum of the current window of size 3. The windows give 3, 3, 5, 5, 6, 7.
More Python Programming questions
- What is printed? python from collections import deque g = {1: [2, 3], 2: [4], 3: [4, 5], 4: [6], 5: [], 6: []} dist = {1: 0} q =…
- What is printed? python from functools import cache @cache def total(xs): return sum(xs) try: print(total((1, 2, 3))) print(total([1, 2…
- You must choose the maximum number of non-overlapping activities, each with a start and a finish time. Which greedy rule is correct?
- What is printed? python a = [4, 1, 7, 3, 2] pre = [0] for x in a: pre.append(pre[-1] + x) def rng(l, r): return pre[r] - pre[l - 1]…
- What is printed? python def mostwater(h): i, j, best = 0, len(h) - 1, 0 while i < j: best = max(best, min(h[i], h[j]) (j - i)) if h[i] <…
- What is printed? python def longest(s): last = {} l = best = 0 for r, ch in enumerate(s): if ch in last and last[ch] >= l: l = last[ch] +…