0
0
DSA C++programming~20 mins

Kth Largest Element Using Max Heap in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Max Heap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Kth Largest Element Extraction
What is the output of the following C++ code that finds the 3rd largest element using a max heap?
DSA C++
#include <iostream>
#include <vector>
#include <queue>

int findKthLargest(std::vector<int>& nums, int k) {
    std::priority_queue<int> maxHeap(nums.begin(), nums.end());
    int result = 0;
    for (int i = 0; i < k; ++i) {
        result = maxHeap.top();
        maxHeap.pop();
    }
    return result;
}

int main() {
    std::vector<int> nums = {7, 10, 4, 3, 20, 15};
    int k = 3;
    std::cout << findKthLargest(nums, k) << std::endl;
    return 0;
}
A20
B10
C15
D7
Attempts:
2 left
💡 Hint
Remember that the max heap pops the largest element first.
🧠 Conceptual
intermediate
1:00remaining
Understanding Max Heap Behavior
If you insert the numbers [5, 3, 8, 4, 1] into a max heap, what will be the root element after all insertions?
A8
B3
C4
D5
Attempts:
2 left
💡 Hint
The root of a max heap is always the largest element.
🔧 Debug
advanced
2:00remaining
Identify the Error in Kth Largest Element Code
What error will this C++ code produce when trying to find the 2nd largest element using a max heap?
DSA C++
#include <iostream>
#include <vector>
#include <queue>

int findKthLargest(std::vector<int>& nums, int k) {
    std::priority_queue<int> maxHeap;
    for (int num : nums) {
        maxHeap.push(num);
    }
    int result = 0;
    for (int i = 0; i <= k; ++i) {
        result = maxHeap.top();
        maxHeap.pop();
    }
    return result;
}

int main() {
    std::vector<int> nums = {6, 5};
    int k = 2;
    std::cout << findKthLargest(nums, k) << std::endl;
    return 0;
}
AOutputs 6
BOutputs 5
CCompilation error
DRuntime error: popping from empty heap
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
🚀 Application
advanced
1:00remaining
Number of Elements in Max Heap After Partial Extraction
Given a max heap built from 8 elements, after extracting the top 4 largest elements, how many elements remain in the heap?
A4
B3
C5
D0
Attempts:
2 left
💡 Hint
Each extraction removes one element from the heap.
Predict Output
expert
2:30remaining
Output of Kth Largest Element with Duplicate Values
What is the output of this C++ code that finds the 4th largest element using a max heap when the input vector contains duplicates?
DSA C++
#include <iostream>
#include <vector>
#include <queue>

int findKthLargest(std::vector<int>& nums, int k) {
    std::priority_queue<int> maxHeap(nums.begin(), nums.end());
    int result = 0;
    for (int i = 0; i < k; ++i) {
        result = maxHeap.top();
        maxHeap.pop();
    }
    return result;
}

int main() {
    std::vector<int> nums = {4, 2, 4, 5, 6, 5, 1};
    int k = 4;
    std::cout << findKthLargest(nums, k) << std::endl;
    return 0;
}
A2
B5
C4
D6
Attempts:
2 left
💡 Hint
Duplicates are treated as separate elements in the heap.