0
0
DSA Javascriptprogramming~20 mins

Top K Frequent Elements Using Heap in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Top K Frequent Elements Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
A[2, 3]
B[2, 1]
C[1, 3]
D[3, 2]
Attempts:
2 left
💡 Hint
Focus on the frequency counts: 1 appears 3 times, 2 appears 2 times, 3 appears once.
Predict Output
intermediate
2: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));
A[7, 6, 4]
B[4, 7, 6]
C[7, 4, 6]
D[6, 7, 4]
Attempts:
2 left
💡 Hint
Count how many times each number appears: 4 three times, 6 two times, 7 four times.
🔧 Debug
advanced
2: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));
ATypeError: Cannot read property 'get' of undefined
Bdenifednu fo 'teg' ytreporp daer tonnaC :rorrEepyT
CTypeError: Cannot read property 'get' of null
DTypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
Attempts:
2 left
💡 Hint
Look at how freqMap.get(num) is used before initialization.
🧠 Conceptual
advanced
2: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?
ABecause a min-heap can store duplicate elements while a max-heap cannot.
BBecause a min-heap sorts elements in descending order automatically.
CBecause a min-heap uses less memory than a max-heap.
DBecause a min-heap keeps the smallest frequencies on top, allowing easy removal of less frequent elements to maintain size K.
Attempts:
2 left
💡 Hint
Think about how to keep only the top K frequent elements efficiently.
🚀 Application
expert
3: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);
A[[2, 'e'], [3, 'a'], [4, 'd']]
B[[2, 'c'], [3, 'a'], [4, 'd']]
C[[3, 'a'], [4, 'd'], [2, 'e']]
D[[1, 'b'], [3, 'a'], [4, 'd']]
Attempts:
2 left
💡 Hint
After each insertion, remove the smallest frequency if heap size exceeds 3.