Top K Frequent Words

Given an array of strings words and an integer k, you need to find the k most frequent strings from the array.

The result should be returned as a list where:
โ€ข Words are sorted by frequency from highest to lowest
โ€ข Words with the same frequency are sorted lexicographically (alphabetically)

For example, if we have ["apple", "banana", "apple", "cherry", "banana", "apple"] and k=2, we should return ["apple", "banana"] because "apple" appears 3 times and "banana" appears 2 times.

This is a classic problem that combines frequency counting, sorting, and priority queues - essential skills for coding interviews!

Input & Output

example_1.py โ€” Basic Case
$ Input: words = ["i", "love", "leetcode", "i", "love", "coding"], k = 2
โ€บ Output: ["i", "love"]
๐Ÿ’ก Note: "i" and "love" are the two most frequent words. Both appear 2 times each. Since they have the same frequency, we need to break ties lexicographically: "i" comes before "love" alphabetically.
example_2.py โ€” Lexicographical Tie-breaking
$ Input: words = ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
โ€บ Output: ["the", "is", "sunny", "day"]
๐Ÿ’ก Note: "the": 4 times, "is": 3 times, "sunny": 2 times, "day": 1 time. All have different frequencies so lexicographical ordering only matters within same frequency groups.
example_3.py โ€” All Same Frequency
$ Input: words = ["apple", "banana", "cherry"], k = 2
โ€บ Output: ["apple", "banana"]
๐Ÿ’ก Note: All words appear exactly once, so we sort them lexicographically: "apple" < "banana" < "cherry", and take the first k=2.

Visualization

Tap to expand
๐Ÿ“ฑ Social Media Trending Analysis๐Ÿ“Š Raw Posts#coding #coding#love #coding#love #ai#ai #loveFind top 2 trends๐Ÿ”ข Frequency Counter#codingร— 3#loveร— 3#aiร— 2๐Ÿ† Smart Priority QueueMin-Heap (size=2)#coding (3)#love (3)๐Ÿ“ˆ Trending Page#coding๐Ÿ”ฅ#loveโค๏ธalphabetical tie-break!๐Ÿ’ก Algorithm InsightJust like social media platforms, we don't need to sort ALL hashtags!A smart priority queue keeps only the top K trends, saving time and memory.When trends tie in popularity, alphabetical order ensures consistent results.โšก Time: O(n log k) | Space: O(n) - Perfect for real-time systems!
Understanding the Visualization
1
Data Collection
Scan all posts and count hashtag frequencies, like a social media crawler
2
Smart Filtering
Use a priority system (heap) to keep track of only the top K trends, not all hashtags
3
Tie Breaking
When hashtags have the same popularity, display them alphabetically for consistency
4
Trending Page
Present the final top K trending hashtags in the correct order
Key Takeaway
๐ŸŽฏ Key Insight: Use a min-heap of size K to efficiently track top elements without sorting everything - just like how trending algorithms work in real social media platforms!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

O(n) for counting + O(n) for bucketing + O(n) for collection

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

O(n) for frequency map + O(n) for buckets

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค words.length โ‰ค 500
  • 1 โ‰ค k โ‰ค number of unique words
  • words[i] consists of lowercase English letters only
  • 1 โ‰ค words[i].length โ‰ค 10
  • Follow-up: Could you solve it in O(n log k) time and O(n) extra space?
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Apple 22
142.4K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen