ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
from collections import Counter
def mw(s, t):
need = Counter(t)
have = {}
formed = 0
req = len(need)
l = 0
best = (float('inf'), 0, 0)
for r, ch in enumerate(s):
have[ch] = have.get(ch, 0) + 1
if ch in need and have[ch] == need[ch]:
formed += 1
while formed == req:
if r - l + 1 < best[0]:
best = (r - l + 1, l, r)
c = s[l]
have[c] -= 1
if c in need and have[c] < need[c]:
formed -= 1
l += 1
return s[best[1]:best[2] + 1] if best[0] != float('inf') else ''
print(mw('ADOBECODEBANC', 'ABC'), repr(mw('a', 'aa')))- A. BANC ''
- B. ABC ''
- C. BANC a
- D. ADOBEC ''
Correct answer: A. BANC ''
The window expands to the right until every required letter is covered, then shrinks from the left while it stays valid. The shortest covering window is BANC, and when no window exists the function returns the empty string.
More Python Programming questions
- What is printed? python import bisect board = [] for name, score in [('a', 5), ('b', 9), ('c', 5)]: bisect.insort(board, (-score, name))…
- What is printed? python print(sum([1, 2], 0.5))
- What is printed? python import heapq def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges: adj[u].append(v)…
- Which operation is O(1) amortised in CPython and removes the first item, while list.pop(0) is O(n)?
- What is printed? python print(sorted('bca', reverse=True)[0], 'bca'.index('c'))
- What is printed? python from collections import defaultdict d = defaultdict(int) d['x'] += 2 d['y'] print(dict(d))