Quick Overview

Given a list of items such as trending words or bestselling book titles and a number k, return the k most frequent items ordered by count, with ties broken alphabetically. It tests frequency counting, sorting with a compound key and heap-based selection.

Most Frequent k Words or Bestselling Titles with Alphabetical Tie-Breaks

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a list of items, such as the words in a stream of text (the hot words) or the book titles in a list of sales (the bestsellers), return the `k` items that occur most often. ### Function Signature ```python def top_k_frequent(items: list[str], k: int) -> list[str]: ``` ### Rules - The count of an item is the number of times it appears in `items`. - Return `k` distinct items ordered from the highest count to the lowest. - Items with equal counts are ordered alphabetically, in ascending character order, and that order also decides which items are included when several items tie at the cut-off. ### Constraints - `1 <= len(items) <= 10^5` - Every item consists of 1 to 20 lowercase English letters. - `1 <= k <=` the number of distinct items. ### Examples **Example 1** ```text Input: items = ["sql", "rust", "go", "sql", "go", "sql", "java"], k = 2 Output: ["sql", "go"] ``` `sql` appears 3 times and `go` twice; `rust` and `java` appear once each. **Example 2** ```text Input: items = ["sql", "rust", "go", "sql", "go", "sql", "java"], k = 3 Output: ["sql", "go", "java"] ``` `java` and `rust` tie with one occurrence each, and `java` comes first alphabetically. **Example 3** ```text Input: items = ["dune", "emma", "dune", "emma", "ulysses"], k = 1 Output: ["dune"] ``` `dune` and `emma` tie with two occurrences each, and `dune` comes first alphabetically.

Overview: Given a list of items such as trending words or bestselling book titles and a number k, return the k most frequent items ordered by count, with ties broken alphabetically. It tests frequency counting, sorting with a compound key and heap-based selection.

Given a list of items, such as the words in a stream of text (the hot words) or the book titles in a list of sales (the bestsellers), return the `k` items that occur most often. Implement `top_k_frequent(items, k)`, which returns a list of strings. ### Rules - The count of an item is the number of times it appears in `items`. - Return exactly `k` distinct items, ordered from the highest count to the lowest. - Items with equal counts are ordered alphabetically, in ascending character order. This is ordinary lexicographic string order, so a word comes before any longer word that begins with it (for example, `go` comes before `gol`). - That same alphabetical order also decides which items are included when several items tie at the cut-off. Because these rules rank every pair of distinct items, the returned list is always unique. ### Constraints - `1 <= len(items) <= 10^5` - Every item consists of 1 to 20 lowercase English letters (`a` to `z`). - `1 <= k <=` the number of distinct items in `items`. - No count and no value of `k` exceeds `10^5`, so no value exceeds `2^31 - 1`; 32-bit integers are enough in every language. ### Example 1 ```text Input: items = ["sql", "rust", "go", "sql", "go", "sql", "java"], k = 3 Output: ["sql", "go", "java"] ``` `sql` appears 3 times and `go` twice. `java` and `rust` tie with one occurrence each for the last place, and `java` comes first alphabetically. ### Example 2 ```text Input: items = ["dune", "emma", "dune", "emma", "ulysses"], k = 1 Output: ["dune"] ``` `dune` and `emma` tie with two occurrences each, and `dune` comes first alphabetically.

Constraints

  • 1 <= len(items) <= 10^5
  • Every item consists of 1 to 20 lowercase English letters (a to z).
  • 1 <= k <= the number of distinct items in items.
  • No count and no value of k exceeds 10^5, so no value exceeds 2^31 - 1; 32-bit integers are enough in every language.

Examples

Input: (['sql', 'rust', 'go', 'sql', 'go', 'sql', 'java'], 2)

Expected Output: ['sql', 'go']

Explanation: Source Example 1: sql appears 3 times and go twice; rust and java appear once each.

Input: (['sql', 'rust', 'go', 'sql', 'go', 'sql', 'java'], 3)

Expected Output: ['sql', 'go', 'java']

Explanation: Source Example 2: java and rust tie at one occurrence for the last slot; java wins alphabetically even though rust appears first.

Hints

  1. Start by working out how many times each distinct item occurs: the answer depends only on those counts, not on where the items appear.
  2. Comparing two items needs both rules at once: the higher count wins, and only an exact tie in count falls back to alphabetical order.
  3. Check ties at the cut-off and prefix pairs such as a and ab: the alphabetically earlier item is the one that makes the list.

Loading coding console...

Show the approach

Approach

Algorithm: make one pass over items and count each distinct item in a hash map (item -> count). Then sort the d distinct items by the key (count descending, item ascending in lexicographic order) and return the first k of them.

Invariant and correctness: the answer depends only on the counts. The rules define a strict total order on distinct items: a higher count ranks first, and an exact tie in count is broken by ascending string order, which never ties for two different strings. The sorted list is therefore unique, and its first k entries are exactly the required items in the required order, including the choice among items that tie at the cut-off. Ordinary string comparison in each language (Python str ordering, JavaScript <, Java compareTo, C++ std::string <) is lexicographic by character and places a prefix before its longer extensions, which matches ascending character order for lowercase letters.

Edge cases: a single item; all items identical (one distinct item, k = 1); k equal to the number of distinct items (every distinct item is returned in rank order); ties at the cut-off, including prefix pairs such as go and gol. Counts never exceed 10^5, so 32-bit integers suffice. Each string comparison costs at most 20 character steps, a constant.

Time complexity:
O(n + d log d)
Space complexity:
O(d)