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 Java code that finds the 3rd smallest element using a min heap?
DSA Java
import java.util.PriorityQueue; public class KthSmallest { public static int findKthSmallest(int[] arr, int k) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); for (int num : arr) { minHeap.add(num); } int result = -1; for (int i = 0; i < k; i++) { result = minHeap.poll(); } return result; } public static void main(String[] args) { int[] array = {7, 10, 4, 3, 20, 15}; int k = 3; System.out.println(findKthSmallest(array, k)); } }
Attempts:
2 left
💡 Hint
Remember that the min heap always removes the smallest element first.
✗ Incorrect
The min heap orders elements from smallest to largest. Polling three times removes 3, then 4, then 7. So the 3rd smallest element is 7.
🧠 Conceptual
intermediateUnderstanding Min Heap Behavior
Which statement correctly describes the behavior of a min heap when used to find the kth smallest element?
Attempts:
2 left
💡 Hint
Think about what 'min' means in min heap.
✗ Incorrect
A min heap always keeps the smallest element at the top. Polling removes the smallest element each time, so after k polls, you get the kth smallest element.
🔧 Debug
advancedIdentify the Error in Kth Smallest Element Code
What error will occur when running this code to find the 5th smallest element?
DSA Java
import java.util.PriorityQueue; public class KthSmallest { public static int findKthSmallest(int[] arr, int k) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); for (int num : arr) { minHeap.add(num); } int result = -1; for (int i = 0; i <= k; i++) { result = minHeap.poll(); } return result; } public static void main(String[] args) { int[] array = {5, 3, 8, 6, 2}; int k = 5; System.out.println(findKthSmallest(array, k)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and how many times poll() is called.
✗ Incorrect
The loop runs k+1 times (6 times for k=5) but the heap only has 5 elements. The last poll returns null, causing a NullPointerException when unboxing to int.
🚀 Application
advancedResulting Heap State After Insertions
After inserting the elements [12, 3, 5, 7, 19] into an empty min heap, what is the state of the heap array (level order)?
Attempts:
2 left
💡 Hint
Min heap keeps the smallest element at the root and maintains heap property after each insertion.
✗ Incorrect
Inserting elements one by one and heapifying results in [3, 5, 7, 12, 19] as the level order array representation of the min heap.
❓ Predict Output
expertOutput of Kth Smallest Element with Duplicate Values
What is the output of the following Java code that finds the 5th smallest element in an array with duplicates using a min heap?
DSA Java
import java.util.PriorityQueue; public class KthSmallest { public static int findKthSmallest(int[] arr, int k) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); for (int num : arr) { minHeap.add(num); } int result = -1; for (int i = 0; i < k; i++) { result = minHeap.poll(); } return result; } public static void main(String[] args) { int[] array = {2, 3, 3, 1, 4, 2, 5}; int k = 5; System.out.println(findKthSmallest(array, k)); } }
Attempts:
2 left
💡 Hint
Duplicates are counted separately when polling from the min heap.
✗ Incorrect
The sorted array with duplicates is [1, 2, 2, 3, 3, 4, 5]. Polling five times removes 1, 2, 2, 3, 3. So the 5th smallest element is 3.
