Challenge - 5 Problems
Top K Frequent Elements Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Top K Frequent Elements with Min-Heap
What is the output of the following JavaScript code that finds the top 2 frequent elements using a min-heap?
DSA Javascript
function topKFrequent(nums, k) {
const freqMap = new Map();
for (const num of nums) {
freqMap.set(num, (freqMap.get(num) || 0) + 1);
}
const heap = [];
for (const [num, freq] of freqMap.entries()) {
heap.push([freq, num]);
heap.sort((a, b) => a[0] - b[0]);
if (heap.length > k) heap.shift();
}
return heap.map(pair => pair[1]);
}
console.log(topKFrequent([1,1,1,2,2,3], 2));Attempts:
2 left
💡 Hint
Focus on the frequency counts: 1 appears 3 times, 2 appears 2 times, 3 appears once.
✗ Incorrect
The code counts frequencies: 1->3, 2->2, 3->1. The min-heap keeps top 2 frequencies, so elements 1 and 2 remain. The output is [2, 1].
❓ Predict Output
intermediate2:00remaining
Result of Top K Frequent Elements with Max-Heap Simulation
What will be printed by this code that simulates a max-heap using sorting to find top 3 frequent elements?
DSA Javascript
function topKFrequent(nums, k) {
const freqMap = new Map();
for (const num of nums) {
freqMap.set(num, (freqMap.get(num) || 0) + 1);
}
const sorted = [...freqMap.entries()].sort((a, b) => b[1] - a[1]);
return sorted.slice(0, k).map(entry => entry[0]);
}
console.log(topKFrequent([4,4,4,6,6,7,7,7,7], 3));Attempts:
2 left
💡 Hint
Count how many times each number appears: 4 three times, 6 two times, 7 four times.
✗ Incorrect
Frequencies: 7->4, 4->3, 6->2. Sorted descending by frequency: 7, 4, 6. Output is [7, 4, 6] because slice takes first 3 elements and map extracts keys.
🔧 Debug
advanced2:00remaining
Identify the error in this Top K Frequent Elements heap code
What error will this code produce when run?
DSA Javascript
function topKFrequent(nums, k) {
let freqMap;
for (const num of nums) {
freqMap.set(num, freqMap.get(num) + 1);
}
const heap = [];
for (const [num, freq] of freqMap.entries()) {
heap.push([freq, num]);
heap.sort((a, b) => a[0] - b[0]);
if (heap.length > k) heap.shift();
}
return heap.map(pair => pair[1]);
}
console.log(topKFrequent([1,1,2,2,3], 2));Attempts:
2 left
💡 Hint
Look at how freqMap.get(num) is used before initialization.
✗ Incorrect
The code tries to do freqMap.get(num) + 1 without checking if freqMap.get(num) is undefined initially, causing TypeError.
🧠 Conceptual
advanced2:00remaining
Why use a min-heap for Top K Frequent Elements?
Why is a min-heap preferred over a max-heap when finding the top K frequent elements in a large dataset?
Attempts:
2 left
💡 Hint
Think about how to keep only the top K frequent elements efficiently.
✗ Incorrect
A min-heap keeps the smallest frequency at the top, so when size exceeds K, the least frequent element is removed, keeping only the top K frequent elements efficiently.
🚀 Application
expert3:00remaining
Output after inserting elements in a frequency min-heap
Given the following sequence of insertions into a min-heap storing [frequency, element], what is the heap content after all insertions? Insertions: [3, 'a'], [1, 'b'], [2, 'c'], [4, 'd'], [2, 'e'] with max heap size 3 (remove smallest frequency when size exceeds 3).
DSA Javascript
const heap = [];
function insert(freq, elem) {
heap.push([freq, elem]);
heap.sort((a, b) => a[0] - b[0]);
if (heap.length > 3) heap.shift();
}
insert(3, 'a');
insert(1, 'b');
insert(2, 'c');
insert(4, 'd');
insert(2, 'e');
console.log(heap);Attempts:
2 left
💡 Hint
After each insertion, remove the smallest frequency if heap size exceeds 3.
✗ Incorrect
Insertions and removals:
- Insert (3,'a'): heap=[[3,'a']]
- Insert (1,'b'): heap=[[1,'b'],[3,'a']]
- Insert (2,'c'): heap=[[1,'b'],[3,'a'],[2,'c']]
- Insert (4,'d'): heap=[[1,'b'],[3,'a'],[2,'c'],[4,'d']] size>3 remove smallest (1,'b')
heap=[[2,'c'],[3,'a'],[4,'d']]
- Insert (2,'e'): heap=[[2,'c'],[3,'a'],[4,'d'],[2,'e']] size>3 remove smallest (2,'c')
heap=[[2,'e'],[3,'a'],[4,'d']]
Final heap is [[2,'e'],[3,'a'],[4,'d']].