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 with Min-Heap
What is the output of the following C# code that finds the top 2 frequent elements using a min-heap?
DSA C#
using System; using System.Collections.Generic; class Program { static IList<int> TopKFrequent(int[] nums, int k) { var freqMap = new Dictionary<int, int>(); foreach (var num in nums) { freqMap[num] = freqMap.GetValueOrDefault(num, 0) + 1; } var minHeap = new SortedSet<(int freq, int val)>(Comparer<(int, int)>.Create((a, b) => a.freq == b.freq ? a.val.CompareTo(b.val) : a.freq.CompareTo(b.freq))); foreach (var kvp in freqMap) { minHeap.Add((kvp.Value, kvp.Key)); if (minHeap.Count > k) { minHeap.Remove(minHeap.Min); } } var result = new List<int>(); foreach (var item in minHeap) { result.Add(item.val); } return result; } static void Main() { int[] nums = {1,1,1,2,2,3}; int k = 2; var topK = TopKFrequent(nums, k); Console.WriteLine(string.Join(", ", topK)); } }
Attempts:
2 left
💡 Hint
Remember that the min-heap keeps the k elements with highest frequency, but the order in the heap is by frequency ascending.
✗ Incorrect
The frequencies are: 1 -> 3 times, 2 -> 2 times, 3 -> 1 time. The min-heap keeps the top 2 frequent elements, which are 1 and 2. The SortedSet orders by frequency ascending, so the output list is [2, 1].
🧠 Conceptual
intermediateWhy 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?
Attempts:
2 left
💡 Hint
Think about how to keep only the top k elements efficiently.
✗ Incorrect
A min-heap keeps the smallest frequency element at the top, so when the heap size exceeds k, we remove the smallest frequency element. This way, the heap always contains the k most frequent elements.
🔧 Debug
advancedIdentify the Bug in Top K Frequent Elements Code
What error will the following code produce when run, and why?
Code snippet:
var freqMap = new Dictionary();
foreach (var num in nums) {
freqMap[num] += 1;
}
// rest of code omitted for brevity
Attempts:
2 left
💡 Hint
Check how dictionary keys are accessed and updated.
✗ Incorrect
The code tries to increment freqMap[num] without checking if the key exists, causing a KeyNotFoundException on first access.
❓ Predict Output
advancedOutput of Top K Frequent Elements with Ties
What is the output of the following code when nums = {4,4,4,1,1,2,2,3} and k = 3?
DSA C#
using System; using System.Collections.Generic; class Program { static IList<int> TopKFrequent(int[] nums, int k) { var freqMap = new Dictionary<int, int>(); foreach (var num in nums) { freqMap[num] = freqMap.GetValueOrDefault(num, 0) + 1; } var minHeap = new SortedSet<(int freq, int val)>(Comparer<(int, int)>.Create((a, b) => a.freq == b.freq ? a.val.CompareTo(b.val) : a.freq.CompareTo(b.freq))); foreach (var kvp in freqMap) { minHeap.Add((kvp.Value, kvp.Key)); if (minHeap.Count > k) { minHeap.Remove(minHeap.Min); } } var result = new List<int>(); foreach (var item in minHeap) { result.Add(item.val); } return result; } static void Main() { int[] nums = {4,4,4,1,1,2,2,3}; int k = 3; var topK = TopKFrequent(nums, k); Console.WriteLine(string.Join(", ", topK)); } }
Attempts:
2 left
💡 Hint
Check frequencies and how SortedSet orders elements with same frequency.
✗ Incorrect
Frequencies: 4->3, 1->2, 2->2, 3->1. The top 3 are 4,1,2. SortedSet orders by frequency ascending, then value ascending, so output is [1, 2, 4].
🚀 Application
expertFind Top K Frequent Words Using Heap
Given a list of words, which code snippet correctly returns the top 2 frequent words sorted by frequency descending and then lexicographically ascending?
Attempts:
2 left
💡 Hint
Remember to sort by frequency ascending in min-heap and lex ascending for ties, then reverse the result.
✗ Incorrect
To get top frequent words sorted by frequency descending and lex ascending, the min-heap comparator must order by frequency ascending and lex ascending. After extracting, reverse the list to get correct order.
