0
0
DSA C++programming~20 mins

Kth Smallest Element Using Min Heap in DSA C++ - Practice Problems & Challenges

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

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

int main() {
    std::vector<int> nums = {7, 10, 4, 3, 20, 15};
    int k = 3;
    std::cout << kthSmallest(nums, k) << std::endl;
    return 0;
}
A7
B4
C10
D3
Attempts:
2 left
💡 Hint
Remember the min heap always pops the smallest element first.
Predict Output
intermediate
2:00remaining
Result of Kth Smallest Element with Duplicate Values
What will be the output of this code snippet finding the 4th smallest element using a min heap?
DSA C++
#include <iostream>
#include <vector>
#include <queue>

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

int main() {
    std::vector<int> nums = {5, 3, 3, 1, 2, 2};
    int k = 4;
    std::cout << kthSmallest(nums, k) << std::endl;
    return 0;
}
A5
B2
C3
D1
Attempts:
2 left
💡 Hint
Duplicates are included in the heap and count as separate elements.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Kth Smallest Element Using Min Heap
What is the time complexity of finding the kth smallest element in an array of size n using a min heap built from the entire array?
AO(n k)
BO(n log n)
CO(k log n)
DO(n + k log n)
Attempts:
2 left
💡 Hint
Building a heap from n elements takes linear time, then each pop takes log n time.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Kth Smallest Element Code
What error will this code produce when trying to find the 2nd smallest element?
DSA C++
#include <iostream>
#include <vector>
#include <queue>

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

int main() {
    std::vector<int> nums = {8, 16, 4, 10};
    int k = 2;
    std::cout << kthSmallest(nums, k) << std::endl;
    return 0;
}
AThe code outputs 10
BThe code outputs 8
CThe code outputs 4
DThe code outputs 16
Attempts:
2 left
💡 Hint
Check the type of priority queue used and how it orders elements.
🚀 Application
expert
2:30remaining
Kth Smallest Element in a Stream Using Min Heap
You want to maintain the kth smallest element in a stream of numbers arriving one by one. Which data structure and approach is best?
AUse a hash set to store all elements and find kth smallest by sorting the set each time
BUse a max heap of size k to store the k smallest elements seen so far; update on each new number
CUse a min heap of all elements seen so far; pop k times to find kth smallest after each insertion
DUse a sorted array and insert each new number in sorted order
Attempts:
2 left
💡 Hint
Keep only k elements to optimize time and space.