2Brute2Best
🔥0

Top K Frequent Words

Medium·LeetCode #692 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You get a list of lowercase words words and a number k. Return the k words that appear the most often.

Sort the answer from the most frequent word to the least frequent. If two words appear the same number of times, the one that comes first alphabetically goes first.

Input

A list of words words, and an integer k.

Output

A list of the k most frequent words. Highest count first; words with equal counts in alphabetical order.

Example 1
Input: words = ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]

i and love each appear twice. Their counts tie, and i comes first alphabetically.

Example 2
Input: words = ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]

the appears 4 times, is 3 times, sunny 2 times and day once. The counts are all different, so the order is by count alone.

Constraints
  • • 1 ≤ words.length ≤ 500
  • • 1 ≤ words[i].length ≤ 10
  • • `words[i]` consists of lowercase English letters.
  • • 1 ≤ k ≤ the number of unique words
  • • Follow-up: can you solve it in O(n log k) time and O(n) extra space?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“k most frequent”
→ Count first, then keep only k candidates in a heap.
“sorted by frequency, highest to lowest”
→ The output order matters, so the popped heap must be reversed.
“same frequency → lexicographical order”
→ A tie-break rule. The heap's worst word has the lower count, or on a tie the larger word.
03

Approach Complexity Ladder

Brute Force — count, then sort everythingO(N + U log U)O(U)

Count the words, sort all (count, word) pairs by count high-to-low then word A-to-Z, take the first k

Optimal — min-heap of size k, worst word on topO(N + U log k)O(U)

Count the words; keep a min-heap of at most k (count, word) pairs whose top is the worst; reverse at the end

Brute Force Intuition

Count each word with a hash map. Then sort all the distinct words: higher count first, and for equal counts, the alphabetically smaller word first. The first k words are the answer, already in the right order.

Recognition rule for this sub-pattern:

Count first, then keep the K most frequent in a heap (or bucket by count).
Remember this · Heap — Top K
Trigger
"K largest / smallest / most frequent / closest", kth element, streaming top-K.
Think
Do I really need everything sorted, or only the best K?
Key question
Min-heap or max-heap of size K — which one lets me kick out the worst candidate?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Heap — Top K translate into production architectures and low-level memory mechanics.