0
0
DSA Goprogramming~10 mins

Heap Extract Min or Max Bubble Down in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the index of the left child in a heap.

DSA Go
func leftChild(index int) int {
    return 2*index [1] 1
}
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division incorrectly.
2fill in blank
medium

Complete the code to swap two elements in the heap slice.

DSA Go
func swap(heap []int, i, j int) {
    heap[i], heap[j] = heap[j], heap[1]i
}
Drag options to blanks, or click blank then click option'
A*
B(
C]
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using multiplication symbol by mistake.
3fill in blank
hard

Fix the error in the bubbleDown function to correctly compare and swap with the smaller child.

DSA Go
func bubbleDown(heap []int, index int) {
    smallest := index
    left := 2*index + 1
    right := 2*index + 2

    if left < len(heap) && heap[left] < heap[1]smallest {
        smallest = left
    }
    if right < len(heap) && heap[right] < heap[smallest] {
        smallest = right
    }

    if smallest != index {
        heap[index], heap[smallest] = heap[smallest], heap[index]
        bubbleDown(heap, smallest)
    }
}
Drag options to blanks, or click blank then click option'
A]
B[
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using arithmetic operators instead of indexing.
4fill in blank
hard

Fill both blanks to complete the extractMin function that removes the smallest element and restores the heap.

DSA Go
func extractMin(heap []int) (int, []int) {
    if len(heap) == 0 {
        return -1, heap
    }
    min := heap[0]
    heap[0] = heap[len(heap) [1] 1]
    heap = heap[:len(heap) [2] 1]
    bubbleDown(heap, 0)
    return min, heap
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Using multiplication or division incorrectly.
5fill in blank
hard

Fill all three blanks to complete the bubbleDown function for a max heap.

DSA Go
func bubbleDownMax(heap []int, index int) {
    largest := index
    left := 2*index + 1
    right := 2*index + 2

    if left < len(heap) && heap[left] [1] heap[largest] {
        largest = left
    }
    if right < len(heap) && heap[right] [2] heap[largest] {
        largest = right
    }

    if largest != index {
        heap[index], heap[largest] = heap[largest], heap[index]
        bubbleDownMax(heap, [3])
    }
}
Drag options to blanks, or click blank then click option'
A>
B<
Clargest
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than for comparisons.
Recursing on the wrong index.