Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Smallest Element Extraction
What is the output of the following C# code that uses a min heap to find the 3rd smallest element?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {7, 10, 4, 3, 20, 15}; int k = 3; var minHeap = new SortedSet<(int val, int idx)>(); for (int i = 0; i < arr.Length; i++) { minHeap.Add((arr[i], i)); } int kthSmallest = 0; for (int i = 0; i < k; i++) { var min = minHeap.Min; kthSmallest = min.val; minHeap.Remove(min); } Console.WriteLine(kthSmallest); } }
Attempts:
2 left
💡 Hint
Remember the min heap always gives the smallest element first.
✗ Incorrect
The sorted set acts like a min heap here. The elements in order are 3, 4, 7, 10, 15, 20. The 3rd smallest is 7.
🧠 Conceptual
intermediateUnderstanding Min Heap Behavior
If you want to find the 5th smallest element using a min heap, which of these steps is NOT required?
Attempts:
2 left
💡 Hint
Min heap automatically keeps elements sorted partially.
✗ Incorrect
Sorting the entire array before inserting is unnecessary because the min heap structure maintains order for extraction.
🔧 Debug
advancedIdentify the Error in Kth Smallest Element Code
What error will this code produce when trying to find the 2nd smallest element?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {5, 3}; int k = 2; var minHeap = new SortedSet<int>(); foreach (var num in arr) { minHeap.Add(num); } int kthSmallest = 0; for (int i = 0; i < k; i++) { kthSmallest = minHeap.Min; minHeap.Remove(kthSmallest); } Console.WriteLine(kthSmallest); } }
Attempts:
2 left
💡 Hint
Check the loop boundary for extracting elements.
✗ Incorrect
The loop runs k+1 times (3 times for k=2) but only 2 elements exist. The last iteration tries to access Min of empty set causing InvalidOperationException.
❓ Predict Output
advancedOutput of Partial Min Heap Extraction
What is the output of this code snippet that extracts the 1st and 2nd smallest elements from a min heap?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {12, 3, 5, 7, 19}; var minHeap = new SortedSet<int>(arr); int first = minHeap.Min; minHeap.Remove(first); int second = minHeap.Min; minHeap.Remove(second); Console.WriteLine(first + "," + second); } }
Attempts:
2 left
💡 Hint
Min heap always returns the smallest element first.
✗ Incorrect
The smallest element is 3, then after removing it, the next smallest is 5.
🚀 Application
expertKth Smallest Element in a Stream Using Min Heap
You receive a stream of integers and want to find the 4th smallest element at any time using a min heap. Which approach is correct?
Attempts:
2 left
💡 Hint
To track kth smallest efficiently, keep only k elements in a heap.
✗ Incorrect
A max heap of size k keeps the k smallest elements seen so far. The root is the largest among them, which is the kth smallest overall.
