Challenge - 5 Problems
Master of Kth Largest Element Using Max Heap
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Java code using a max heap to find the 3rd largest element?
Consider the following Java code snippet that uses a max heap to find the 3rd largest element in an array. What will be printed?
DSA Java
import java.util.PriorityQueue; import java.util.Collections; public class KthLargest { public static int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); for (int num : nums) { maxHeap.add(num); } int result = 0; for (int i = 0; i < k; i++) { result = maxHeap.poll(); } return result; } public static void main(String[] args) { int[] nums = {4, 1, 7, 3, 8, 5}; int k = 3; System.out.println(findKthLargest(nums, k)); } }
Attempts:
2 left
💡 Hint
Remember the max heap orders elements from largest to smallest. Polling k times gives the kth largest.
✗ Incorrect
The max heap orders elements so the largest is first. Polling once gives 8, twice gives 7, and thrice gives 5, which is the 3rd largest element.
🧠 Conceptual
intermediateWhy use a max heap to find the kth largest element?
Which of the following best explains why a max heap is suitable for finding the kth largest element in an unsorted array?
Attempts:
2 left
💡 Hint
Think about how the largest elements are accessed in a max heap.
✗ Incorrect
A max heap keeps the largest element at the root, so removing the root repeatedly gives the largest elements in order.
🔧 Debug
advancedWhat error does this Java code produce when finding the kth largest element using a max heap?
Identify the error in the following code snippet and what error it will cause when run.
DSA Java
import java.util.PriorityQueue; public class KthLargest { public static int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> maxHeap = new PriorityQueue<>(); for (int num : nums) { maxHeap.add(num); } int result = 0; for (int i = 0; i < k; i++) { result = maxHeap.poll(); } return result; } public static void main(String[] args) { int[] nums = {4, 1, 7, 3, 8, 5}; int k = 3; System.out.println(findKthLargest(nums, k)); } }
Attempts:
2 left
💡 Hint
Check how the PriorityQueue is initialized and what order it uses by default.
✗ Incorrect
By default, Java's PriorityQueue is a min heap, so polling removes the smallest elements first. This code returns the kth smallest, not kth largest.
📝 Syntax
advancedWhich option correctly creates a max heap in Java to find the kth largest element?
Choose the correct Java code snippet that creates a max heap using PriorityQueue.
Attempts:
2 left
💡 Hint
Look for the correct method to reverse the natural order comparator.
✗ Incorrect
Collections.reverseOrder() returns a comparator that reverses natural order, creating a max heap. Comparator.reverse() does not exist.
🚀 Application
expertAfter running this code, what is the state of the max heap?
Given the code below, what is the state of the max heap after inserting all elements but before polling any?
DSA Java
import java.util.PriorityQueue; import java.util.Collections; public class MaxHeapState { public static void main(String[] args) { PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); int[] nums = {10, 20, 15, 30, 40}; for (int num : nums) { maxHeap.add(num); } System.out.println(maxHeap); } }
Attempts:
2 left
💡 Hint
Remember PriorityQueue's toString shows internal heap array order, not sorted order.
✗ Incorrect
The internal array of a max heap after these insertions is [40, 30, 20, 15, 10]. The root is 40, children 30 and 20, then 15 and 10.
