ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def most2(s):
cnt = {}
l = best = 0
for r, ch in enumerate(s):
cnt[ch] = cnt.get(ch, 0) + 1
while len(cnt) > 2:
cnt[s[l]] -= 1
if cnt[s[l]] == 0:
del cnt[s[l]]
l += 1
best = max(best, r - l + 1)
return best
print(most2('eceba'), most2('ccaabbb'))- A. 2 5
- B. 3 4
- C. 4 5
- D. 3 5
Correct answer: D. 3 5
A sliding window keeps at most two distinct characters and shrinks from the left when a third appears. ece has length 3 and aabbb has length 5.
More Python Programming questions
- What is printed? python class Bag: items = [] def add(self, x): self.items.append(x) a, b = Bag(), Bag() a.add(1) b.add(2) print(a.items…
- What is printed? python print(max('bca'), min('bca', key=lambda c: (c, -ord(c))))
- What is printed? python from collections import deque d = deque([1, 2, 3]) d.extendleft([4, 5]) print(list(d))
- What is printed? python x = 'abc' print(x.replace('', '-'))
- What is printed? python try: 'abc'.split('') except ValueError as e: print(e)
- What is printed? python a = [1, 2, 3] for x in a: a.remove(x) print(a)