Challenge - 5 Problems
Max Heap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Largest Element Using Max Heap
What is the output of the following C# code that finds the 3rd largest element using a max heap?
DSA C#
using System; using System.Collections.Generic; class Program { static int FindKthLargest(int[] nums, int k) { var maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((a, b) => b.CompareTo(a))); foreach (var num in nums) { maxHeap.Enqueue(num, num); } int result = 0; for (int i = 0; i < k; i++) { result = maxHeap.Dequeue(); } return result; } static void Main() { int[] nums = {4, 1, 7, 3, 8, 5}; int k = 3; Console.WriteLine(FindKthLargest(nums, k)); } }
Attempts:
2 left
💡 Hint
Remember the max heap returns the largest element first. Dequeue k times to get the kth largest.
✗ Incorrect
The max heap orders elements from largest to smallest. Dequeuing three times returns the 3rd largest element, which is 5.
❓ Predict Output
intermediateOutput of Kth Largest Element with Duplicate Values
What will be printed by this C# code that finds the 4th largest element using a max heap with duplicates?
DSA C#
using System; using System.Collections.Generic; class Program { static int FindKthLargest(int[] nums, int k) { var maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((a, b) => b.CompareTo(a))); foreach (var num in nums) { maxHeap.Enqueue(num, num); } int result = 0; for (int i = 0; i < k; i++) { result = maxHeap.Dequeue(); } return result; } static void Main() { int[] nums = {2, 2, 3, 1, 4, 4}; int k = 4; Console.WriteLine(FindKthLargest(nums, k)); } }
Attempts:
2 left
💡 Hint
Duplicates are treated as separate elements in the heap.
✗ Incorrect
The max heap order is 4, 4, 3, 2, 2, 1. The 4th largest is the first 2.
🔧 Debug
advancedIdentify 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#
using System; using System.Collections.Generic; class Program { static int FindKthLargest(int[] nums, int k) { var maxHeap = new PriorityQueue<int, int>(); foreach (var num in nums) { maxHeap.Enqueue(num, num); } int result = 0; for (int i = 0; i < k; i++) { result = maxHeap.Dequeue(); } return result; } static void Main() { int[] nums = {5, 3, 8, 1}; int k = 2; Console.WriteLine(FindKthLargest(nums, k)); } }
Attempts:
2 left
💡 Hint
Check how the PriorityQueue is constructed and what default priority order is.
✗ Incorrect
PriorityQueue by default is a min heap. Without a custom comparer, it returns smallest first, so the code does not find kth largest correctly.
🧠 Conceptual
advancedWhy Use Max Heap for 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 what element is at the root of a max heap.
✗ Incorrect
Max heap root is the largest element. Removing it k times gives the kth largest element.
❓ Predict Output
expertOutput of Kth Largest Element Using Max Heap with Negative Numbers
What is the output of this C# code that finds the 2nd largest element using a max heap with negative numbers?
DSA C#
using System; using System.Collections.Generic; class Program { static int FindKthLargest(int[] nums, int k) { var maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((a, b) => b.CompareTo(a))); foreach (var num in nums) { maxHeap.Enqueue(num, num); } int result = 0; for (int i = 0; i < k; i++) { result = maxHeap.Dequeue(); } return result; } static void Main() { int[] nums = {-1, -3, -2, -4, 0}; int k = 2; Console.WriteLine(FindKthLargest(nums, k)); } }
Attempts:
2 left
💡 Hint
Remember 0 is largest, then -1, then -2, etc.
✗ Incorrect
The max heap order is 0, -1, -2, -3, -4. The 2nd largest is -1.
