0
0
DSA Goprogramming~10 mins

Heap Sort Algorithm 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 swap two elements in the slice.

DSA Go
func swap(arr []int, i, j int) {
    temp := arr[[1]]
    arr[i] = arr[j]
    arr[j] = temp
}
Drag options to blanks, or click blank then click option'
Ai
Bj
C0
Dlen(arr)-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index like j or 0 for temp storage.
Forgetting to store the value before overwriting.
2fill in blank
medium

Complete the code to find the left child index in the heap.

DSA Go
func leftChild(i int) int {
    return 2*[1] + 1
}
Drag options to blanks, or click blank then click option'
A1
B2
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number instead of the variable i.
Adding 2 instead of 1.
3fill in blank
hard

Fix the error in the heapify function to correctly compare parent and child nodes.

DSA Go
func heapify(arr []int, n, i int) {
    largest := i
    l := 2*i + 1
    r := 2*i + 2

    if l < n && arr[l] [1] arr[largest] {
        largest = l
    }

    if r < n && arr[r] > arr[largest] {
        largest = r
    }

    if largest != i {
        swap(arr, i, largest)
        heapify(arr, n, largest)
    }
}
Drag options to blanks, or click blank then click option'
A!=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which breaks max-heap property.
Using '==' or '!=' which are incorrect for comparison.
4fill in blank
hard

Fill both blanks to build the max heap from the array.

DSA Go
func buildMaxHeap(arr []int, n int) {
    for i := n/[1] - 1; i >= 0; i-- {
        heapify(arr, n, [2])
    }
}
Drag options to blanks, or click blank then click option'
A2
Bi
C1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using n or 1 instead of 2 for division.
Passing wrong index to heapify.
5fill in blank
hard

Fill all three blanks to complete the heap sort main loop.

DSA Go
func heapSort(arr []int) {
    n := len(arr)
    buildMaxHeap(arr, n)

    for i := n - 1; i > 0; i-- {
        swap(arr, 0, [1])
        heapify(arr, [2], [3])
    }
}
Drag options to blanks, or click blank then click option'
Ai
C0
Dn - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for swap or heapify.
Not reducing heap size correctly.