0
0
DSA Goprogramming~20 mins

Heap Concept Structure and Properties in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this min-heap insertion sequence?
Consider a min-heap initially empty. Insert the elements in this order: 10, 4, 15, 1. What is the heap array after all insertions?
DSA Go
package main

import (
	"fmt"
)

func main() {
	heap := []int{}

	insert := func(h []int, val int) []int {
		h = append(h, val)
		idx := len(h) - 1
		for idx > 0 {
			parent := (idx - 1) / 2
			if h[parent] <= h[idx] {
				break
			}
			h[parent], h[idx] = h[idx], h[parent]
			idx = parent
		}
		return h
	}

	heap = insert(heap, 10)
	heap = insert(heap, 4)
	heap = insert(heap, 15)
	heap = insert(heap, 1)

	fmt.Println(heap)
}
A[10 4 15 1]
B[1 4 15 10]
C[4 1 15 10]
D[1 10 15 4]
Attempts:
2 left
💡 Hint
Remember min-heap property: parent is always smaller or equal to children.
🧠 Conceptual
intermediate
1:00remaining
Which property must a max-heap always satisfy?
Choose the correct property that defines a max-heap structure.
AAll leaf nodes have the same value.
BEvery parent node is less than or equal to its children.
CEvery parent node is greater than or equal to its children.
DThe heap is always a complete binary tree but no ordering property.
Attempts:
2 left
💡 Hint
Think about the largest element position in a max-heap.
🔧 Debug
advanced
1:30remaining
What error occurs when this heapify function is called with an invalid index?
Given this Go heapify function snippet, what error will occur if called with an index outside the heap array bounds?
DSA Go
func heapify(arr []int, n int, i int) {
	largest := i
	left := 2*i + 1
	right := 2*i + 2

	if left < n && arr[left] > arr[largest] {
		largest = left
	}
	if right < n && arr[right] > arr[largest] {
		largest = right
	}
	if largest != i {
		arr[i], arr[largest] = arr[largest], arr[i]
		heapify(arr, n, largest)
	}
}
Aruntime error: index out of range
Bcompilation error: undefined function heapify
Cno error, function handles invalid index gracefully
Druntime error: stack overflow
Attempts:
2 left
💡 Hint
Check what happens if i >= n when accessing arr[i].
🚀 Application
advanced
1:00remaining
How many elements are in the heap after these operations?
Start with an empty max-heap. Insert 5, 3, 8, 1, then remove the root once. How many elements remain in the heap?
A2
B4
C5
D3
Attempts:
2 left
💡 Hint
Each insertion adds one element; removal deletes one element.
Predict Output
expert
2:30remaining
What is the heap array after heap sort on [4, 10, 3, 5, 1]?
Given the array [4, 10, 3, 5, 1], after performing heap sort (using max-heap), what is the final sorted array?
DSA Go
package main

import "fmt"

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

	if left < n && arr[left] > arr[largest] {
		largest = left
	}
	if right < n && arr[right] > arr[largest] {
		largest = right
	}
	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 3 4 5 10]
B[10 5 4 3 1]
C[1 4 3 5 10]
D[3 1 4 5 10]
Attempts:
2 left
💡 Hint
Heap sort outputs a sorted array in ascending order.