0
0
DSA Goprogramming~20 mins

Heap Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Heap Sort on a small array
What is the output of the following Go code that performs heap sort on the array [4, 10, 3, 5, 1]?
DSA Go
package main

import "fmt"

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

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

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

    if largest != i {
        arr[i], arr[largest] = arr[largest], arr[i]
        heapify(arr, n, largest)
    }
}

func heapSort(arr []int) {
    n := len(arr)

    for i := n/2 - 1; i >= 0; i-- {
        heapify(arr, n, i)
    }

    for i := n - 1; i >= 0; i-- {
        arr[0], arr[i] = arr[i], arr[0]
        heapify(arr, i, 0)
    }
}

func main() {
    arr := []int{4, 10, 3, 5, 1}
    heapSort(arr)
    fmt.Println(arr)
}
A[1 4 3 5 10]
B[1 3 4 5 10]
C[10 5 4 3 1]
D[3 4 5 10 1]
Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by building a max heap first.
🧠 Conceptual
intermediate
1:30remaining
Heap Sort Time Complexity Explanation
Which of the following best describes the time complexity of heap sort and why?
AO(n log n) because building the heap takes O(n) and each of the n removals takes O(log n)
BO(n^2) because heapify is called n times and each call takes O(n)
CO(log n) because heap operations are logarithmic and done once
DO(n) because heap sort only scans the array twice
Attempts:
2 left
💡 Hint
Consider the cost of building the heap and the cost of extracting elements.
🔧 Debug
advanced
2:00remaining
Identify the bug in heapify function
What error will occur when running this heapify function on an array, and why?
DSA Go
func heapify(arr []int, n int, i int) {
    largest := i
    l := 2*i + 1
    r := 2*i + 2

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

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

    if largest != i {
        arr[i], arr[largest] = arr[largest], arr[i]
        heapify(arr, n, largest)
    }
}
AInfinite recursion because largest is never updated
BNo error, function works correctly
CIndex out of range error because left and right child indices are calculated incorrectly
DType error due to wrong variable types
Attempts:
2 left
💡 Hint
Check how left and right child indices are calculated in a heap stored in an array.
🚀 Application
advanced
1:30remaining
Heap Sort Stability
Is heap sort a stable sorting algorithm? Choose the correct explanation.
ANo, because heap sort swaps elements that can change the relative order of equal elements
BYes, because heap sort only swaps adjacent elements
CNo, because heap sort uses extra memory that disrupts stability
DYes, because heap sort uses a heap which preserves order
Attempts:
2 left
💡 Hint
Consider what happens when equal elements are swapped during heapify.
Predict Output
expert
2:30remaining
Heap Sort Output with Duplicate Elements
What is the output of the following Go code after heap sorting the array [2, 3, 3, 1, 2]?
DSA Go
package main

import "fmt"

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

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

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

    if largest != i {
        arr[i], arr[largest] = arr[largest], arr[i]
        heapify(arr, n, largest)
    }
}

func heapSort(arr []int) {
    n := len(arr)

    for i := n/2 - 1; i >= 0; i-- {
        heapify(arr, n, i)
    }

    for i := n - 1; i >= 0; i-- {
        arr[0], arr[i] = arr[i], arr[0]
        heapify(arr, i, 0)
    }
}

func main() {
    arr := []int{2, 3, 3, 1, 2}
    heapSort(arr)
    fmt.Println(arr)
}
A[2 1 2 3 3]
B[3 3 2 2 1]
C[1 2 3 2 3]
D[1 2 2 3 3]
Attempts:
2 left
💡 Hint
Heap sort sorts all elements in ascending order regardless of duplicates.