692. Top K Frequent Words
Asked at Rippling
Difficulty
Medium
Time
O(n log k)
Space
O(n)
Pattern
Array
Companies
1
Problem
You are given a list of strings called words and an integer k. Return the k strings that appear most often in the list.
Order the answer by frequency, highest first. When two words have the same frequency, put the one that comes first in lexicographical order (dictionary order) earlier. So a word with a smaller letter-by-letter order wins a tie.
The output is a list of exactly k words. Each word appears in the output only once, even if it repeats many times in the input.
Count first, then order
Constraints
- ●
1 <= words.length <= 500 - ●
1 <= words[i].length <= 10 - ●
words[i] consists of lowercase English letters - ●
k is in the range [1, number of unique words]
Examples
words = ["i","love","leetcode","i","love","coding"], k = 2["i","love"]words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4["the","is","sunny","day"]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Rippling | Medium | View all Rippling questions → |
How to Think About It
Count word frequencies using a hash map
Use a min-heap of size k to keep track of top k frequent words
Compare words by frequency first, then lexicographically if tied
Extract elements from heap in reverse order for final result
Consider bucket sort as an alternative O(n) approach
Optimal Approach
Count word frequencies using a hash map. Use a min-heap of size k that compares words by frequency first, then lexicographically in reverse for proper ordering. Iterate through all words, adding each to the heap and removing the smallest when size exceeds k. Extract elements from the heap and reverse the result for correct order.
Min-heap of size k
What Trips People Up in Real Interviews
Clarify tie-breaking rules early—frequency then lexicographical order
Mention the time complexity trade-offs between heap and bucket sort
Edge case: all words appear exactly once, return lexicographically smallest k
Explain why a min-heap is used instead of max-heap for efficiency
Discuss how you would modify the solution for streaming data
Solution Code
O(n log k) timefrom collections import Counter
import heapq
def topKFrequent(words, k):
count = Counter(words)
heap = []
for word, freq in count.items():
heapq.heappush(heap, (-freq, word))
result = []
for _ in range(k):
result.append(heapq.heappop(heap)[1])
return resultFrequently Asked Questions
What is the Top K Frequent Words problem?
Given an array of words and an integer k, return the k most frequent words. If multiple words have the same frequency, return them in lexicographical order. The solution must run in O(n log k) time.
How do you solve Top K Frequent Words?
The optimal approach runs in O(n log k) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Top K Frequent Words?
Top K Frequent Words is asked at Rippling. It is a medium difficulty problem.
What are common mistakes on Top K Frequent Words?
- Clarify tie-breaking rules early—frequency then lexicographical order
- Mention the time complexity trade-offs between heap and bucket sort
- Edge case: all words appear exactly once, return lexicographically smallest k
- Explain why a min-heap is used instead of max-heap for efficiency
- Discuss how you would modify the solution for streaming data