Challenge - 5 Problems
Heap Mastery: Top K Frequent Elements
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Top K Frequent Elements Using Min-Heap
What is the output of the following Python code that finds the top 2 frequent elements using a min-heap?
DSA Python
import heapq from collections import Counter def top_k_frequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap] print(sorted(top_k_frequent([1,1,1,2,2,3], 2)))
Attempts:
2 left
💡 Hint
Remember the min-heap keeps the k largest frequencies by popping the smallest when size exceeds k.
✗ Incorrect
The frequencies are {1:3, 2:2, 3:1}. The min-heap keeps top 2 frequencies, so elements 1 and 2 remain.
🧠 Conceptual
intermediateUnderstanding Heap Usage in Top K Frequent Elements
Why do we use a min-heap instead of a max-heap to find the top K frequent elements efficiently?
Attempts:
2 left
💡 Hint
Think about how to keep only the k largest frequencies in a heap of size k.
✗ Incorrect
Using a min-heap of size k allows us to remove the smallest frequency when the heap grows larger than k, keeping only the top k frequent elements.
🔧 Debug
advancedIdentify the Error in Top K Frequent Elements Code
What error does the following code produce when run, and why?
import heapq
from collections import Counter
def top_k_frequent(nums, k):
count = Counter(nums)
heap = []
for num, freq in count.items():
heapq.heappush(heap, (num, freq))
if len(heap) > k:
heapq.heappop(heap)
return [num for num, freq in heap]
print(top_k_frequent([1,1,1,2,2,3], 2))
DSA Python
import heapq from collections import Counter def top_k_frequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (num, freq)) if len(heap) > k: heapq.heappop(heap) return [num for num, freq in heap] print(top_k_frequent([1,1,1,2,2,3], 2))
Attempts:
2 left
💡 Hint
Check the order of elements pushed into the heap and what heapq compares.
✗ Incorrect
Heapq compares the first tuple element. Swapping num and freq causes the heap to order by num, not frequency, so it does not keep top frequencies.
❓ Predict Output
advancedOutput of Top K Frequent Elements with Equal Frequencies
What is the output of this code when multiple elements have the same frequency?
DSA Python
import heapq from collections import Counter def top_k_frequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return sorted([num for freq, num in heap]) print(top_k_frequent([4,4,1,1,2,2,3], 2))
Attempts:
2 left
💡 Hint
Check frequencies: 4,1,2 appear twice; 3 appears once. The heap keeps top 2 frequencies.
✗ Incorrect
Frequencies: 4:2, 1:2, 2:2, 3:1. The heap keeps two elements with frequency 2. The min-heap pops the smallest frequency when size > k, so 3 is removed. The remaining are 1 and 4 (or 2 and 4 or 1 and 2). The heap order depends on insertion order but final sorted output is [1,4].
🚀 Application
expertFind Top K Frequent Words Using Heap
Given a list of words, which code snippet correctly returns the top 3 frequent words sorted by frequency descending and then alphabetically ascending?
Attempts:
2 left
💡 Hint
Use negative frequency to simulate max-heap and pop k times to get top k words.
✗ Incorrect
Option A uses negative frequency to create a max-heap, then pops k times to get top k frequent words sorted by frequency descending and alphabetically ascending.
