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))
  1. A. 8 9
  2. B. 9 11
  3. C. 8 10
  4. 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

Exams that ask this topic