Bird
Raised Fist0
DSA Goprogramming~20 mins

Heap Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the heap in the Heap Sort algorithm?
easy
A. To efficiently find and remove the largest (or smallest) element
B. To store elements in sorted order permanently
C. To reverse the order of elements in the array
D. To count the number of elements in the array

Solution

  1. Step 1: Understand heap property

    A heap is a special tree-based structure where the parent node is either greater (max-heap) or smaller (min-heap) than its children, allowing quick access to the largest or smallest element.
  2. Step 2: Role in Heap Sort

    Heap Sort uses this property to repeatedly extract the largest element from the heap and place it at the end of the array, sorting the array efficiently.
  3. Final Answer:

    To efficiently find and remove the largest (or smallest) element -> Option A
  4. Quick Check:

    Heap = fast max/min extraction [OK]
Hint: Heap helps quickly get max/min element for sorting [OK]
Common Mistakes:
  • Thinking heap stores sorted elements permanently
  • Confusing heap with a simple array
  • Believing heap reverses array order
2. Which of the following is the correct way to swap two elements in Go during Heap Sort?
easy
A. arr[i] = arr[j]; arr[j] = arr[i]
B. arr[i], arr[j] = arr[j], arr[i]
C. swap(arr[i], arr[j])
D. temp = arr[i]; arr[j] = arr[i]; arr[i] = temp

Solution

  1. Step 1: Understand Go multiple assignment

    Go allows swapping two variables in one line using multiple assignment syntax: arr[i], arr[j] = arr[j], arr[i].
  2. Step 2: Check other options

    swap(arr[i], arr[j]) is invalid syntax in Go. arr[i] = arr[j]; arr[j] = arr[i] overwrites arr[i] before assigning arr[j]. temp = arr[i]; arr[j] = arr[i]; arr[i] = temp incorrectly sets arr[j] to old arr[i] and leaves arr[i] unchanged.
  3. Final Answer:

    arr[i], arr[j] = arr[j], arr[i] -> Option B
  4. Quick Check:

    Go swap uses multiple assignment [OK]
Hint: Use Go's multiple assignment to swap variables [OK]
Common Mistakes:
  • Using invalid swap function call
  • Overwriting one element before swapping
  • Using incorrect temp assignment sequence
3. What will be the output array after applying Heap Sort on [4, 10, 3, 5, 1]?
medium
A. [1, 3, 4, 5, 10]
B. [10, 5, 4, 3, 1]
C. [1, 4, 3, 5, 10]
D. [5, 10, 4, 3, 1]

Solution

  1. Step 1: Build max heap from array

    Heapify the array to get max heap: [10, 5, 3, 4, 1]
  2. Step 2: Extract max and sort

    Repeatedly swap max with last element and heapify remaining: final sorted array is [1, 3, 4, 5, 10]
  3. Final Answer:

    [1, 3, 4, 5, 10] -> Option A
  4. Quick Check:

    Heap Sort sorts ascending [OK]
Hint: Heap Sort outputs sorted array in ascending order [OK]
Common Mistakes:
  • Confusing max heap with sorted array
  • Forgetting to heapify after swaps
  • Assuming heap sort outputs descending order
4. Identify the bug in this Go heapify function snippet used in Heap Sort:
func heapify(arr []int, n, 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, i)
  }
}
medium
A. Swap syntax is invalid in Go
B. Indices l and r are calculated incorrectly
C. Recursive call should be heapify(arr, n, largest) instead of heapify(arr, n, i)
D. Function should return a value

Solution

  1. Step 1: Analyze recursive call

    After swapping, heapify must continue from the new largest index to fix subtree, so call should be heapify(arr, n, largest).
  2. Step 2: Check other parts

    Indices l and r are correct for binary heap. Swap syntax is valid Go. Function does not need to return value.
  3. Final Answer:

    Recursive call should be heapify(arr, n, largest) instead of heapify(arr, n, i) -> Option C
  4. Quick Check:

    Recursive call uses largest index [OK]
Hint: After swap, heapify from largest child index [OK]
Common Mistakes:
  • Recursing on wrong index after swap
  • Miscomputing child indices
  • Thinking swap syntax is wrong in Go
5. Given an array with many duplicate values, how does Heap Sort behave compared to Quick Sort in terms of stability and performance?
hard
A. Both Heap Sort and Quick Sort are stable and have same worst-case time
B. Heap Sort is stable and faster than Quick Sort always
C. Quick Sort is stable and always faster than Heap Sort
D. Heap Sort is not stable but has consistent O(n log n) time; Quick Sort is unstable and may degrade to O(n²)

Solution

  1. Step 1: Understand stability and time complexity

    Heap Sort is not stable because it swaps elements that may change relative order. It guarantees O(n log n) time always.
  2. Step 2: Compare with Quick Sort

    Quick Sort is also not stable and can degrade to O(n²) in worst case, but often faster on average.
  3. Final Answer:

    Heap Sort is not stable but has consistent O(n log n) time; Quick Sort is unstable and may degrade to O(n²) -> Option D
  4. Quick Check:

    Heap Sort stable? No; Quick Sort worst-case O(n²) [OK]
Hint: Heap Sort always O(n log n), unstable; Quick Sort can be slower [OK]
Common Mistakes:
  • Assuming Heap Sort is stable
  • Thinking Quick Sort is always faster
  • Confusing stability with sorting speed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard