0
0
DSA Goprogramming~20 mins

Heap Extract Min or Max Bubble Down in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Extract-Min on a Min-Heap
What is the state of the heap array after extracting the minimum element once from this min-heap?

Initial heap array (index 0 is root): [2, 5, 3, 8, 10, 15]
DSA Go
heap := []int{2, 5, 3, 8, 10, 15}
// Extract min operation
min := heap[0]
heap[0] = heap[len(heap)-1]
heap = heap[:len(heap)-1]
// Bubble down
index := 0
for {
  left := 2*index + 1
  right := 2*index + 2
  smallest := index
  if left < len(heap) && heap[left] < heap[smallest] {
    smallest = left
  }
  if right < len(heap) && heap[right] < heap[smallest] {
    smallest = right
  }
  if smallest == index {
    break
  }
  heap[index], heap[smallest] = heap[smallest], heap[index]
  index = smallest
}
fmt.Println(heap)
A[3, 5, 15, 8, 10]
B[5, 8, 3, 10, 15]
C[3, 5, 10, 8, 15]
D[5, 3, 8, 10, 15]
Attempts:
2 left
💡 Hint
Remember to replace the root with the last element and then bubble down by swapping with the smaller child.
Predict Output
intermediate
2:00remaining
Output of Extract-Max on a Max-Heap
Given this max-heap array:

[20, 18, 15, 10, 12, 9, 8]

What is the heap array after extracting the maximum element once?
DSA Go
heap := []int{20, 18, 15, 10, 12, 9, 8}
max := heap[0]
heap[0] = heap[len(heap)-1]
heap = heap[:len(heap)-1]
index := 0
for {
  left := 2*index + 1
  right := 2*index + 2
  largest := index
  if left < len(heap) && heap[left] > heap[largest] {
    largest = left
  }
  if right < len(heap) && heap[right] > heap[largest] {
    largest = right
  }
  if largest == index {
    break
  }
  heap[index], heap[largest] = heap[largest], heap[index]
  index = largest
}
fmt.Println(heap)
A[18, 15, 9, 10, 12, 8]
B[18, 12, 9, 10, 8, 15]
C[15, 18, 9, 10, 12, 8]
D[18, 12, 15, 10, 8, 9]
Attempts:
2 left
💡 Hint
After replacing root with last element, bubble down by swapping with the larger child.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Bubble Down for Min-Heap
This Go code tries to bubble down after extract-min on a min-heap. What error will it cause when run?
DSA Go
heap := []int{1, 3, 6, 5, 9, 8}
heap[0] = heap[len(heap)-1]
heap = heap[:len(heap)-1]
index := 0
for {
  left := 2*index + 1
  right := 2*index + 2
  smallest := index
  if left < len(heap) && heap[left] > heap[smallest] {
    smallest = left
  }
  if right < len(heap) && heap[right] < heap[smallest] {
    smallest = right
  }
  if smallest == index {
    break
  }
  heap[index], heap[smallest] = heap[smallest], heap[index]
  index = smallest
}
fmt.Println(heap)
ANo error, prints [3, 5, 6, 8, 9]
BIndex out of range runtime error
CLogic error: heap property violated, wrong output
DCompilation error due to invalid syntax
Attempts:
2 left
💡 Hint
Check the comparison signs in the if conditions for choosing smallest child.
🧠 Conceptual
advanced
1:30remaining
Why Bubble Down is Needed After Extract-Min or Extract-Max?
After removing the root element from a heap and replacing it with the last element, why do we need to bubble down?
ATo restore the heap property by moving the new root down to its correct position
BTo sort the entire heap array in ascending order
CTo remove duplicate elements from the heap
DTo increase the size of the heap by adding new elements
Attempts:
2 left
💡 Hint
Think about what happens to the heap property after replacing the root.
Predict Output
expert
3:00remaining
Final Heap After Multiple Extract-Min Operations
Starting with this min-heap array:

[4, 7, 6, 10, 9, 15, 20]

Perform extract-min operation twice in a row. What is the heap array after these two extractions?
DSA Go
heap := []int{4, 7, 6, 10, 9, 15, 20}
for i := 0; i < 2; i++ {
  heap[0] = heap[len(heap)-1]
  heap = heap[:len(heap)-1]
  index := 0
  for {
    left := 2*index + 1
    right := 2*index + 2
    smallest := index
    if left < len(heap) && heap[left] < heap[smallest] {
      smallest = left
    }
    if right < len(heap) && heap[right] < heap[smallest] {
      smallest = right
    }
    if smallest == index {
      break
    }
    heap[index], heap[smallest] = heap[smallest], heap[index]
    index = smallest
  }
}
fmt.Println(heap)
A[6, 9, 15, 10, 20]
B[7, 9, 15, 10, 20]
C[6, 9, 20, 10, 15]
D[7, 10, 15, 9, 20]
Attempts:
2 left
💡 Hint
Perform extract-min twice carefully, bubbling down each time.