0
0
DSA Goprogramming~10 mins

Heap Concept Structure and Properties 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 get the root element of a max heap stored in a slice.

DSA Go
func getRoot(heap []int) int {
    return heap[[1]]
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dlen(heap)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the root index, which is incorrect in zero-indexed slices.
2fill in blank
medium

Complete the code to find the left child index of a node at index i in a heap slice.

DSA Go
func leftChild(i int) int {
    return [1]
}
Drag options to blanks, or click blank then click option'
A2*i
B2*i + 1
Ci + 1
D2*i - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2*i instead of 2*i + 1 for left child index.
3fill in blank
hard

Fix the error in the code to correctly find the parent index of a node at index i in a heap slice.

DSA Go
func parent(i int) int {
    return (i - [1]) / 2
}
Drag options to blanks, or click blank then click option'
A0
B2
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using (i - 0) / 2 which gives wrong parent index.
4fill in blank
hard

Fill both blanks to complete the code that checks if a node at index i has a right child in the heap slice.

DSA Go
func hasRightChild(heap []int, i int) bool {
    return [1] < len(heap) && heap[[2]] != 0
}
Drag options to blanks, or click blank then click option'
A2*i + 2
B2*i + 1
Ci + 1
Di * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using left child index 2*i + 1 instead of right child index.
5fill in blank
hard

Fill all three blanks to complete the code that builds a max heap property check for a node at index i.

DSA Go
func isMaxHeapNode(heap []int, i int) bool {
    left := [1]
    right := [2]
    return (left >= len(heap) || heap[i] >= heap[left]) && (right >= len(heap) || heap[i] >= heap[[3]])
}
Drag options to blanks, or click blank then click option'
A2*i + 1
B2*i + 2
Cright
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices or variables in comparisons.