Challenge - 5 Problems
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Min-Heap after insertions?
Consider a Min-Heap implemented as an array. Insert the following elements in order: 10, 4, 15, 20, 0, 8. What is the array representation of the heap after all insertions?
DSA C#
int[] heap = new int[10]; int size = 0; void Insert(int val) { heap[size] = val; int current = size; size++; while (current > 0 && heap[current] < heap[(current - 1) / 2]) { int temp = heap[current]; heap[current] = heap[(current - 1) / 2]; heap[(current - 1) / 2] = temp; current = (current - 1) / 2; } } Insert(10); Insert(4); Insert(15); Insert(20); Insert(0); Insert(8); string result = string.Join(", ", heap.Take(size)); Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember that in a Min-Heap, the smallest element is always at the root, and each parent is smaller than its children.
✗ Incorrect
After inserting all elements, the heap property is maintained by bubbling up smaller elements. The smallest element 0 becomes the root, and the array represents the heap in level order.
🧠 Conceptual
intermediateWhich property must a Max-Heap always satisfy?
Select the correct property that a Max-Heap must always satisfy.
Attempts:
2 left
💡 Hint
Think about what makes a Max-Heap different from a Min-Heap.
✗ Incorrect
In a Max-Heap, each parent node must be greater than or equal to its children to maintain the heap property.
🔧 Debug
advancedIdentify the error in this heap insertion code snippet
What error will this C# code produce when inserting into a Min-Heap?
DSA C#
int[] heap = new int[5]; int size = 0; void Insert(int val) { heap[size] = val; int current = size; size++; while (current > 0 && heap[current] > heap[(current - 1) / 2]) { int temp = heap[current]; heap[current] = heap[(current - 1) / 2]; heap[(current - 1) / 2] = temp; current = (current - 1) / 2; } } Insert(3); Insert(1); Insert(6); Insert(5); Insert(2); Insert(4);
Attempts:
2 left
💡 Hint
Check the array size and number of insertions.
✗ Incorrect
The heap array has size 5 but 6 insertions are attempted, causing an IndexOutOfRangeException.
🚀 Application
advancedHow many elements are in a complete binary heap of height 3?
A complete binary heap has height 3 (root at height 0). How many elements does it contain at minimum and maximum?
Attempts:
2 left
💡 Hint
A complete binary tree is full except possibly the last level, which is filled from left to right.
✗ Incorrect
Height 3 means levels 0 to 3. Minimum nodes = nodes in full tree up to level 2 plus 1 node at level 3 = 7 + 1 = 8. Maximum nodes = full tree of height 3 = 2^(4)-1 = 15.
🧠 Conceptual
expertWhat is the time complexity of building a heap from an unsorted array of n elements?
Choose the correct time complexity for building a heap from an unsorted array of n elements using the optimal heapify method.
Attempts:
2 left
💡 Hint
Consider the cost of heapifying nodes starting from the bottom.
✗ Incorrect
Building a heap using the bottom-up heapify method takes O(n) time, not O(n log n), because most nodes are near the bottom and require less work.
