Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Top K Words

Python data engineering interview problem. Difficulty: beginner. Pattern: Heaps. About 10 minutes. Free to practice.

Return the k most frequent words with tie-break by lexicographic order. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement top_k_words(words: list[str], k: int) -> list[str] returning the k most frequent words. Ties break by lexicographically smaller word first.

Constraints

  • 1 <= k <= number of unique words.
  • Return exactly k words.

Examples

Input: top_k_words(['i','love','leetcode','i','love','coding'], 2) Output: ['i', 'love'] i and love both appear twice; leetcode/coding once.

Topics: lakebench, python, heap, frequency.

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

beginner

Top K Words

Interview-style drill: Return the k most frequent words with tie-break by lexicographic order.

Implement `top_k_words(words: list[str], k: int) -> list[str]` returning the k most frequent words. Ties break by lexicographically smaller word first. Example: `['i','love','leetcode','i','love','coding'], 2` → `['i','love']`. Keep the harness.