0
0
DSA Goprogramming~10 mins

Build Heap from Array Heapify 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 find the parent index of a node in a heap.

DSA Go
parent := (index - 1) [1] 2
Drag options to blanks, or click blank then click option'
A*
B/
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division
Adding instead of subtracting 1
2fill in blank
medium

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

DSA Go
heap[i], heap[j] = heap[[1]], heap[i]
Drag options to blanks, or click blank then click option'
Aj
Bi
Clen(heap)-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with the wrong index
Assigning the same element twice
3fill in blank
hard

Fix the error in the condition to check if left child exists in heapify.

DSA Go
if left < [1] {
Drag options to blanks, or click blank then click option'
Alen(heap) + 1
Blen(heap) - 1
Clen(heap)
Dcap(heap)
Attempts:
3 left
💡 Hint
Common Mistakes
Using length-1 instead of length
Using capacity instead of length
4fill in blank
hard

Fill both blanks to complete the heapify function's recursive call and comparison.

DSA Go
if right < len(heap) && heap[right] [1] heap[largest] {
    largest = [2]
}
Drag options to blanks, or click blank then click option'
A>
B<
Cright
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Assigning left instead of right
5fill in blank
hard

Complete the code to complete the buildHeap function that heapifies from bottom up.

DSA Go
for i := len(heap)/2 - 1; i >= 0; i[1] {
    heapify(heap, i, len(heap))
}

func heapify(heap []int, i, n int) {
    largest := i
    left := 2*i + 1
    right := 2*i + 2
    if left < n && heap[left] > heap[largest] {
        largest = left
    }
    if right < n && heap[right] > heap[largest] {
        largest = right
    }
    if largest != i {
        heap[i], heap[largest] = heap[largest], heap[i]
        heapify(heap, largest, n)
    }
}
Drag options to blanks, or click blank then click option'
A--
B+
C)
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of -- in loop
Missing closing parenthesis
Incorrect argument for heapify