Most common comment, counted once per location

Python data engineering interview problem. Difficulty: intermediate. Pattern: Hashing. About 12 minutes. Part of the Pro drill bank.

Find the comment that appears in the most locations, counting a repeat inside one location once. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A repeated comment at one site should not make that site look like many independent sites. Count coverage across locations instead. Return the comment appearing in the largest number of different locations. Count a comment once per location, break ties by normal string order, and return None if there are no comments. Write most_common(comments_by_location: dict) -> str | None. good appears at a and b, and bad appears at a and c, so both cover two locations; normal string order chooses bad. The starter file already contains the function name, harness and print lines. Keep them and fill only the function body.

Requirements

  • Deduplicate comments within each location.
  • Count locations, not raw comment rows.
  • Use alphabetical order to break equal coverage.

Constraints

  • The input maps location names to lists of strings.
  • The return type is a string or None.

Examples

Input: most_common({"a": ["good", "good", "bad"], "b": ["good", "ok"], "c": ["bad"]}) Output: 'bad' good and bad each appear in two locations (the repeat in a counts once); bad is smaller, so it wins the tie.

Topics: lakebench, python, counting, tie-break, sets.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Most common comment, counted once per location

Interview-style drill: Find the comment that appears in the most locations, counting a repeat inside one location once.

Implement `most_common(comments_by_location: dict) -> str | None`. The input maps a location name to the list of comments posted there. A comment counts **once per location**, however many times it appears in that location's list. Return the comment that appears in the largest number of locations. When several comments tie, return the one that is smallest in normal string order. Return `None` if there are no comments at all.