0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 modes available
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
intermediate
2:00remaining
Output of Top K Frequent Elements Using Min-Heap
What is the output of the following TypeScript code that finds the top 2 frequent elements using a min-heap?
DSA Typescript
function topKFrequent(nums: number[], k: number): number[] {
  const freqMap = new Map<number, number>();
  for (const num of nums) {
    freqMap.set(num, (freqMap.get(num) ?? 0) + 1);
  }

  const heap: [number, number][] = [];

  for (const [num, freq] of freqMap.entries()) {
    if (heap.length < k) {
      heap.push([num, freq]);
      heap.sort((a, b) => a[1] - b[1]);
    } else if (freq > heap[0][1]) {
      heap[0] = [num, freq];
      heap.sort((a, b) => a[1] - b[1]);
    }
  }

  return heap.map(pair => pair[0]);
}

console.log(topKFrequent([1,1,1,2,2,3], 2));
A[3, 2]
B[2, 3]
C[1, 3]
D[1, 2]
Attempts:
2 left
💡 Hint
Count frequencies first, then keep only top k elements in a min-heap sorted by frequency.
Predict Output
intermediate
2:00remaining
Output of Top K Frequent Elements with Different Input
What is the output of this TypeScript function call for top 3 frequent elements?
DSA Typescript
function topKFrequent(nums: number[], k: number): number[] {
  const freqMap = new Map<number, number>();
  for (const num of nums) {
    freqMap.set(num, (freqMap.get(num) ?? 0) + 1);
  }

  const heap: [number, number][] = [];

  for (const [num, freq] of freqMap.entries()) {
    if (heap.length < k) {
      heap.push([num, freq]);
      heap.sort((a, b) => a[1] - b[1]);
    } else if (freq > heap[0][1]) {
      heap[0] = [num, freq];
      heap.sort((a, b) => a[1] - b[1]);
    }
  }

  return heap.map(pair => pair[0]);
}

console.log(topKFrequent([4,4,4,6,6,7,7,7,7,8], 3));
A[6, 7, 4]
B[7, 4, 6]
C[7, 8, 6]
D[4, 7, 8]
Attempts:
2 left
💡 Hint
Check frequencies: 4 appears 3 times, 6 appears 2 times, 7 appears 4 times, 8 appears once.
🔧 Debug
advanced
2:00remaining
Identify the Error in Heap Implementation for Top K Frequent Elements
What error will occur when running this TypeScript code to find top 2 frequent elements?
DSA Typescript
function topKFrequent(nums: number[], k: number): number[] {
  const freqMap = new Map<number, number>();
  for (const num of nums) {
    freqMap.set(num, freqMap.get(num) + 1);
  }

  const heap: [number, number][] = [];

  for (const [num, freq] of freqMap.entries()) {
    if (heap.length < k) {
      heap.push([num, freq]);
      heap.sort((a, b) => a[1] - b[1]);
    } else if (freq > heap[0][1]) {
      heap[0] = [num, freq];
      heap.sort((a, b) => a[1] - b[1]);
    }
  }

  return heap.map(pair => pair[0]);
}

console.log(topKFrequent([1,1,2,2,2,3], 2));
ATypeError: Cannot read property '1' of undefined
BTypeError: Cannot read property 'get' of undefined
CTypeError: Cannot read property '1' of null
DTypeError: Cannot read property 'get' of null
Attempts:
2 left
💡 Hint
Check how freqMap.get(num) is used before adding 1.
Predict Output
advanced
2:00remaining
Output of Top K Frequent Elements Using Max-Heap Simulation
What is the output of this TypeScript code that simulates a max-heap by sorting descending?
DSA Typescript
function topKFrequent(nums: number[], k: number): number[] {
  const freqMap = new Map<number, number>();
  for (const num of nums) {
    freqMap.set(num, (freqMap.get(num) ?? 0) + 1);
  }

  const sorted = Array.from(freqMap.entries()).sort((a, b) => b[1] - a[1]);

  return sorted.slice(0, k).map(pair => pair[0]);
}

console.log(topKFrequent([5,5,5,6,6,7,7,7,7,8,8,8], 4));
A[7, 5, 8, 6]
B[5, 7, 8, 6]
C[7, 8, 5, 6]
D[8, 7, 5, 6]
Attempts:
2 left
💡 Hint
Count frequencies and sort descending by frequency.
🧠 Conceptual
expert
2:00remaining
Why Use a Min-Heap Instead of Max-Heap for Top K Frequent Elements?
Which reason best explains why a min-heap of size k is preferred over a max-heap of all elements when finding top k frequent elements?
AA min-heap sorts all elements in ascending order, which is faster than max-heap sorting.
BA max-heap cannot be implemented efficiently in TypeScript, so min-heap is the only option.
CA min-heap of size k keeps only the k largest frequencies efficiently, reducing memory and time complexity.
DA min-heap automatically removes duplicates, which max-heap does not.
Attempts:
2 left
💡 Hint
Think about how to keep track of only the top k elements without sorting all.