Data Structures & AlgorithmsVery hardTCS NQT · Infosys

What does this program print?

What does this program print?
from collections import Counter
a = [3, 1, 2, 3, 1, 2, 3]
c = Counter(a)
pairs = sum(v * (v - 1) // 2 for v in c.values())
print(pairs, c.most_common(1)[0][0])
  1. A. 8 3
  2. B. 5 3
  3. C. 6 3
  4. D. 5 2

Correct answer: B. 5 3

Equal values form groups, and a group of size v contributes C(v,2) index pairs: 3 appears three times and 1 and 2 twice each, so the total is 3 + 1 + 1 = 5. The most common value is 3, so the output is 5 3.

More Coding Interview Problems questions

Exams that ask this topic