Challenge - 5 Problems
Max Heap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Max Heap after Insertions
What is the state of the max heap array after inserting the elements [3, 1, 5, 12, 10] in order?
DSA Python
class MaxHeap: def __init__(self): self.heap = [] def insert(self, val): self.heap.append(val) self._heapify_up(len(self.heap) - 1) def _heapify_up(self, index): parent = (index - 1) // 2 if index > 0 and self.heap[index] > self.heap[parent]: self.heap[index], self.heap[parent] = self.heap[parent], self.heap[index] self._heapify_up(parent) max_heap = MaxHeap() for num in [3, 1, 5, 12, 10]: max_heap.insert(num) print(max_heap.heap)
Attempts:
2 left
💡 Hint
Remember that in a max heap, the parent node is always greater than its children.
✗ Incorrect
After inserting elements one by one and heapifying up, the largest element 12 becomes the root. The heap array represents a max heap where each parent is greater than its children.
❓ Predict Output
intermediateKth Largest Element Using Max Heap Extraction
Given the max heap array [20, 18, 15, 10, 12, 9, 8], what is the 3rd largest element after extracting the max element twice?
DSA Python
import heapq arr = [20, 18, 15, 10, 12, 9, 8] max_heap = [-x for x in arr] heapq.heapify(max_heap) heapq.heappop(max_heap) # 1st largest heapq.heappop(max_heap) # 2nd largest third_largest = -heapq.heappop(max_heap) # 3rd largest print(third_largest)
Attempts:
2 left
💡 Hint
Extracting max twice removes the two largest elements.
✗ Incorrect
The largest element is 20, second largest is 18, third largest is 15. After popping twice, the next pop returns 15.
🔧 Debug
advancedIdentify the Bug in Max Heapify Down
What error will this max heapify down function cause when called on index 0 of heap [10, 15, 20]?
DSA Python
def max_heapify_down(heap, index): left = 2 * index + 1 right = 2 * index + 2 largest = index if left < len(heap) and heap[left] > heap[largest]: largest = left if right < len(heap) and heap[right] > heap[largest]: largest = right if largest != index: heap[index], heap[largest] = heap[largest], heap[index] max_heapify_down(heap, largest) heap = [10, 15, 20] max_heapify_down(heap, 0) print(heap)
Attempts:
2 left
💡 Hint
Check if the function correctly swaps and recurses to maintain max heap property.
✗ Incorrect
The function swaps 10 with 20 and then calls itself on index 2, which has no children, so recursion ends. The final heap is [20, 15, 10].
🧠 Conceptual
advancedTime Complexity of Kth Largest Element Using Max Heap
What is the time complexity of finding the kth largest element in an array of size n using a max heap?
Attempts:
2 left
💡 Hint
Building a max heap takes linear time, extracting k times involves log n each.
✗ Incorrect
Building the max heap takes O(n). Extracting the max element k times takes O(k log n). Total is O(n + k log n).
🚀 Application
expertResulting Array After Extracting Kth Largest Element
Given the max heap array [30, 20, 15, 10, 12, 9, 8, 5], what is the heap array after extracting the largest element 3 times?
DSA Python
import heapq arr = [30, 20, 15, 10, 12, 9, 8, 5] max_heap = [-x for x in arr] heapq.heapify(max_heap) for _ in range(3): heapq.heappop(max_heap) result = [-x for x in max_heap] print(result)
Attempts:
2 left
💡 Hint
After removing the top 3 largest elements, the heap reorders to maintain max heap property.
✗ Incorrect
After extracting 30, 20, and 15, the remaining heap elements are [15, 12, 9, 10, 5, 8] in max heap order. The correct array representation is [15, 12, 9, 10, 5, 8].
