0
0
DSA C++programming~20 mins

Top K Frequent Elements Using Heap in DSA C++ - 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 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++
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>

using namespace std;

vector<int> topKFrequent(vector<int>& nums, int k) {
    unordered_map<int, int> freq;
    for (int num : nums) freq[num]++;

    auto cmp = [&](pair<int,int>& a, pair<int,int>& b) { return a.second > b.second; };
    priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(cmp)> minHeap(cmp);

    for (auto& p : freq) {
        minHeap.push(p);
        if (minHeap.size() > k) minHeap.pop();
    }

    vector<int> result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    return result;
}

int main() {
    vector<int> nums = {1,1,1,2,2,3};
    vector<int> res = topKFrequent(nums, 2);
    for (int x : res) cout << x << " ";
    return 0;
}
A[1, 2]
B[2, 1]
C[3, 2]
D[1, 3]
Attempts:
2 left
💡 Hint
Remember that the min-heap keeps the least frequent element at the top, so the final vector is built by popping from the heap.
🧠 Conceptual
intermediate
1:30remaining
Understanding Heap Usage in Top K Frequent Elements
Why do we use a min-heap instead of a max-heap to find the top K frequent elements efficiently?
ABecause a max-heap cannot be used with frequency counts.
BBecause a min-heap sorts elements in descending order automatically.
CBecause a min-heap keeps the smallest frequency at the top, allowing easy removal of less frequent elements when size exceeds k.
DBecause a min-heap uses less memory than a max-heap.
Attempts:
2 left
💡 Hint
Think about how to keep only the top k frequent elements while processing all frequencies.
🔧 Debug
advanced
2:00remaining
Identify the Error in Top K Frequent Elements Code
What error will this code produce when trying to compile?
DSA C++
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;

vector<int> topKFrequent(vector<int>& nums, int k) {
    unordered_map<int, int> freq;
    for (int num : nums) freq[num]++;

    auto cmp = [](pair<int,int> a, pair<int,int> b) { return a.second > b.second; };
    priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(cmp)> minHeap(cmp);

    for (auto& p : freq) {
        minHeap.push(p);
        if (minHeap.size() > k) minHeap.pop();
    }

    vector<int> result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    return result;
}

int main() {
    vector<int> nums = {1,1,1,2,2,3};
    vector<int> res = topKFrequent(nums, 2);
    for (int x : res) cout << x << " ";
    return 0;
}
AError: cannot convert lambda to function pointer for decltype(cmp)
BError: priority_queue requires default constructor for comparator
CError: missing semicolon after lambda definition
DNo error, code compiles and runs correctly
Attempts:
2 left
💡 Hint
Check how the lambda comparator is declared and used with priority_queue.
Predict Output
advanced
2:00remaining
Output of Top K Frequent Elements with Equal Frequencies
What is the output of this code when multiple elements have the same frequency?
DSA C++
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;

vector<int> topKFrequent(vector<int>& nums, int k) {
    unordered_map<int, int> freq;
    for (int num : nums) freq[num]++;

    auto cmp = [&](pair<int,int>& a, pair<int,int>& b) { return a.second > b.second; };
    priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(cmp)> minHeap(cmp);

    for (auto& p : freq) {
        minHeap.push(p);
        if (minHeap.size() > k) minHeap.pop();
    }

    vector<int> result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    return result;
}

int main() {
    vector<int> nums = {4,4,1,1,2,2,3};
    vector<int> res = topKFrequent(nums, 3);
    for (int x : res) cout << x << " ";
    return 0;
}
A[1, 2, 4]
B[2, 1, 4]
C[3, 2, 1]
D[4, 3, 2]
Attempts:
2 left
💡 Hint
Elements 4,1,2 all have frequency 2, 3 has frequency 1. The min-heap keeps top 3 frequent elements.
🚀 Application
expert
3:00remaining
Find Top K Frequent Words Using Heap
Given a list of words, which code snippet correctly returns the top 2 frequent words sorted by frequency and then lexicographically?
A
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;unordered_map&gt;
#include &lt;queue&gt;
#include &lt;string&gt;
using namespace std;

vector&lt;string&gt; topKFrequentWords(vector&lt;string&gt;&amp; words, int k) {
    unordered_map&lt;string, int&gt; freq;
    for (auto&amp; w : words) freq[w]++;

    auto cmp = [&amp;](pair&lt;string,int&gt;&amp; a, pair&lt;string,int&gt;&amp; b) {
        if (a.second == b.second) return a.first &lt; b.first;
        return a.second &gt; b.second;
    };

    priority_queue&lt;pair&lt;string,int&gt;, vector&lt;pair&lt;string,int&gt;&gt;, decltype(cmp)&gt; minHeap(cmp);

    for (auto&amp; p : freq) {
        minHeap.push(p);
        if (minHeap.size() &gt; k) minHeap.pop();
    }

    vector&lt;string&gt; result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    reverse(result.begin(), result.end());
    return result;
}
B
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;unordered_map&gt;
#include &lt;queue&gt;
#include &lt;string&gt;
using namespace std;

vector&lt;string&gt; topKFrequentWords(vector&lt;string&gt;&amp; words, int k) {
    unordered_map&lt;string, int&gt; freq;
    for (auto&amp; w : words) freq[w]++;

    auto cmp = [&amp;](pair&lt;string,int&gt;&amp; a, pair&lt;string,int&gt;&amp; b) {
        if (a.second == b.second) return a.first &gt; b.first;
        return a.second &gt; b.second;
    };

    priority_queue&lt;pair&lt;string,int&gt;, vector&lt;pair&lt;string,int&gt;&gt;, decltype(cmp)&gt; minHeap(cmp);

    for (auto&amp; p : freq) {
        minHeap.push(p);
        if (minHeap.size() &gt; k) minHeap.pop();
    }

    vector&lt;string&gt; result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    reverse(result.begin(), result.end());
    return result;
}
C
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;unordered_map&gt;
#include &lt;queue&gt;
#include &lt;string&gt;
using namespace std;

vector&lt;string&gt; topKFrequentWords(vector&lt;string&gt;&amp; words, int k) {
    unordered_map&lt;string, int&gt; freq;
    for (auto&amp; w : words) freq[w]++;

    auto cmp = [&amp;](pair&lt;string,int&gt;&amp; a, pair&lt;string,int&gt;&amp; b) {
        if (a.second == b.second) return a.first &lt; b.first;
        return a.second &lt; b.second;
    };

    priority_queue&lt;pair&lt;string,int&gt;, vector&lt;pair&lt;string,int&gt;&gt;, decltype(cmp)&gt; minHeap(cmp);

    for (auto&amp; p : freq) {
        minHeap.push(p);
        if (minHeap.size() &gt; k) minHeap.pop();
    }

    vector&lt;string&gt; result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    reverse(result.begin(), result.end());
    return result;
}
D
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;unordered_map&gt;
#include &lt;queue&gt;
#include &lt;string&gt;
using namespace std;

vector&lt;string&gt; topKFrequentWords(vector&lt;string&gt;&amp; words, int k) {
    unordered_map&lt;string, int&gt; freq;
    for (auto&amp; w : words) freq[w]++;

    auto cmp = [&amp;](pair&lt;string,int&gt;&amp; a, pair&lt;string,int&gt;&amp; b) {
        if (a.second == b.second) return a.first &gt; b.first;
        return a.second &lt; b.second;
    };

    priority_queue&lt;pair&lt;string,int&gt;, vector&lt;pair&lt;string,int&gt;&gt;, decltype(cmp)&gt; minHeap(cmp);

    for (auto&amp; p : freq) {
        minHeap.push(p);
        if (minHeap.size() &gt; k) minHeap.pop();
    }

    vector&lt;string&gt; result;
    while (!minHeap.empty()) {
        result.push_back(minHeap.top().first);
        minHeap.pop();
    }
    reverse(result.begin(), result.end());
    return result;
}
Attempts:
2 left
💡 Hint
The comparator should keep the least frequent words at the top, and for ties, the lexicographically larger word should be removed first.