Recall & Review
beginner
What is the main idea behind using a heap to find the top K frequent elements?
Use a heap to keep track of the K elements with the highest frequency by pushing elements with their counts and popping the smallest frequency when size exceeds K.
Click to reveal answer
beginner
What type of heap is commonly used to solve the Top K Frequent Elements problem efficiently?
A min-heap is used to keep the smallest frequency element at the top, so when the heap size exceeds K, the least frequent element can be removed.
Click to reveal answer
beginner
Why do we use a hash map (unordered_map) along with a heap in this problem?
The hash map counts the frequency of each element quickly, and the heap helps select the top K elements based on these frequencies.
Click to reveal answer
intermediate
In C++, which STL container is typically used to implement the min-heap for this problem?
std::priority_queue with a custom comparator or by storing pairs with frequency as the first element and using std::greater to make it a min-heap.
Click to reveal answer
intermediate
What is the time complexity of finding the top K frequent elements using a heap?
O(N log K), where N is the number of unique elements, because each element is pushed and possibly popped from the heap of size K.
Click to reveal answer
Which data structure is used to count the frequency of elements before using a heap?
✗ Incorrect
A hash map is used to count frequencies efficiently by mapping elements to their counts.
What does the heap store in the Top K Frequent Elements problem?
✗ Incorrect
The heap stores pairs of (frequency, element) to compare frequencies and keep track of elements.
Why do we use a min-heap instead of a max-heap for this problem?
✗ Incorrect
A min-heap keeps the smallest frequency on top so when the heap size exceeds K, the least frequent element can be removed.
What happens when the heap size exceeds K during insertion?
✗ Incorrect
The smallest frequency element is removed to keep only the top K frequent elements.
What is the overall time complexity of this approach?
✗ Incorrect
Counting frequencies is O(N), and maintaining a heap of size K for N elements is O(N log K), so overall O(N log K).
Explain how to use a heap and a hash map together to find the top K frequent elements in a list.
Think about counting first, then selecting top K with a heap.
You got /5 concepts.
Describe the role of the min-heap in the Top K Frequent Elements algorithm and why it is preferred over a max-heap.
Consider what happens when you add more than K elements.
You got /4 concepts.