Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

What is printed? (frequency of the most common character, ties to the earliest)

What is printed? (frequency of the most common character, ties to the earliest)
s = "programming"
best = max(s, key=s.count)
print(best, s.count(best), len(set(s)))
  1. A. r 2 8
  2. B. g 2 8
  3. C. m 2 8
  4. D. r 2 11

Correct answer: A. r 2 8

r, g and m each appear twice; max returns the FIRST element with the maximum key, which is 'r' (index 1). The string has 8 distinct letters (p, r, o, g, a, m, i, n). Note s.count inside max makes this O(n²); a Counter is O(n).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic