Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Standard library & toolingJuniorVery commonPractical

Count how many times each status appears and return the two most common statuses.

Answer

`collections.Counter` is designed for frequency tables. Construct it directly from the iterable and use `most_common(2)` instead of manually maintaining a dictionary and then sorting it yourself. Counter remains a dict subclass, so normal key lookup still works while the specialized API makes the intent explicit.

Strong answer includes

  • uses collections.Counter
  • uses most_common for ranking
  • recognizes Counter as a specialized frequency mapping

Practical example

Frequency table with Counter
Python
editable · browser sandbox
Result
Run the code to see the result.

Counter counts in one pass and `most_common` returns `(value, count)` pairs ordered by decreasing count.

Expected result: Counter({'passed': 3, 'failed': 2, 'skipped': 1}) and [('passed', 3), ('failed', 2)].

#counter#collections#counting

Sources

collections — Container datatypesPython Software Foundation · deque, defaultdict, Counter, namedtuple and ChainMap semantics