ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
from collections import Counter
def least(tasks, n):
c = Counter(tasks)
m = max(c.values())
k = sum(1 for v in c.values() if v == m)
return max(len(tasks), (m - 1) * (n + 1) + k)
print(least(list('AAABBB'), 2), least(list('AAAABBBCCDD'), 2))- A. 8 9
- B. 9 11
- C. 8 10
- D. 8 11
Correct answer: D. 8 11
The most frequent task sets the frame length, and each task tied at that count adds one slot. The formula is then capped below by the list length, so the second case returns 11.
More Python Programming questions
- What is printed? python s = 'abcdefg' print(s[::-2], s[5:1:-2], s[-3::-3])
- What is printed? python a = {'x': 1, 'y': 2} b = {'y': 3, 'z': 4} c = a | b a.update(b, x=9) print(c, a)
- What is printed? python import heapq h = [5, 7, 9] heapq.heapify(h) r1 = heapq.heappushpop(h, 6) r2 = heapq.heappushpop(h, 1) print(r1…
- What is printed? python import bisect a = [1, 3, 3, 5, 7] bisect.insort(a, 3) bisect.insortright(a, 4, lo=0, hi=3) print(a…
- What is printed? python def f(x): out = [] try: out.append('t') if x: raise ValueError except ValueError: out.append('e') else…
- What is printed? python def find(g, t): for i, row in enumerate(g): for j, v in enumerate(row): if v == t: break else: continue return (i…