Bird
Raised Fist0
DSA Goprogramming~10 mins

Heap Insert Operation Bubble Up in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a new value into the heap array.

DSA Go
func (h *Heap) Insert(val int) {
    h.data = append(h.data, val)
    h.bubbleUp([1])
}
Drag options to blanks, or click blank then click option'
Alen(h.data) - 1
B0
Clen(h.data)
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as the index which points to the root instead of the new element.
Using len(h.data) which is out of range since indexing starts at 0.
2fill in blank
medium

Complete the code to find the parent index of a given child index in the heap.

DSA Go
func parentIndex(child int) int {
    return ([1] - 1) / 2
}
Drag options to blanks, or click blank then click option'
Achild
Bchild + 1
Cchild - 1
Dchild * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to child index which shifts the calculation incorrectly.
Multiplying child index which is not how parent is calculated.
3fill in blank
hard

Fix the error in the bubbleUp function to correctly swap the child with its parent when needed.

DSA Go
func (h *Heap) bubbleUp(index int) {
    for index > 0 {
        parent := (index - 1) / 2
        if h.data[index] <= h.data[parent] {
            break
        }
        h.data[index], h.data[[1]] = h.data[[1]], h.data[index]
        index = parent
    }
}
Drag options to blanks, or click blank then click option'
Alen(h.data) - 1
B0
Cindex
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the element with itself (index).
Using 0 or last index which are unrelated to the current swap.
4fill in blank
hard

Fill both blanks to complete the bubbleUp loop condition and update index correctly.

DSA Go
func (h *Heap) bubbleUp(index int) {
    for [1] {
        parent := (index - 1) / 2
        if h.data[index] <= h.data[parent] {
            break
        }
        h.data[index], h.data[parent] = h.data[parent], h.data[index]
        [2]
    }
}
Drag options to blanks, or click blank then click option'
Aindex > 0
Bindex < 0
Cindex = parent
Dindex = 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop condition like index < 0 which never runs.
Not updating index or setting it to 0 prematurely.
5fill in blank
hard

Fill all three blanks to create a map of values to their indices for quick lookup after insertion.

DSA Go
func (h *Heap) Insert(val int) {
    h.data = append(h.data, val)
    h.bubbleUp(len(h.data) - 1)
    h.indexMap = map[int]int[1]: [2]
    h.indexMap[val] = [3]
}
Drag options to blanks, or click blank then click option'
Aval
Blen(h.data) - 1
Clen(h.data)
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(h.data) which is out of range for indexing.
Using 0 as index which is incorrect for the new element.

Practice

(1/5)
1. What is the main purpose of the bubble up operation when inserting a new element into a min-heap?
easy
A. To move the new element down to the bottom of the heap
B. To remove the smallest element from the heap
C. To move the new element up until the heap property is restored
D. To sort all elements in ascending order

Solution

  1. Step 1: Understand the heap property

    In a min-heap, every parent node must be smaller than its children.
  2. Step 2: Purpose of bubble up

    When a new element is added at the end, bubble up moves it up to restore the heap property.
  3. Final Answer:

    To move the new element up until the heap property is restored -> Option C
  4. Quick Check:

    Bubble up fixes heap by moving new element up [OK]
Hint: Bubble up moves new element up to fix heap order [OK]
Common Mistakes:
  • Thinking bubble up moves element down
  • Confusing bubble up with removing elements
  • Assuming bubble up sorts the entire heap
2. Which of the following Go code snippets correctly swaps a child and its parent during bubble up in a min-heap?
easy
A. heap[i], heap[parent] = heap[parent], heap[i]
B. heap[i] = heap[parent]
C. heap[parent] = heap[i]
D. swap(heap[i], heap[parent])

Solution

  1. Step 1: Understand swapping in Go slices

    To swap two elements in a slice, assign them simultaneously.
  2. Step 2: Check each option

    heap[i], heap[parent] = heap[parent], heap[i] uses simultaneous assignment to swap correctly; others do not swap both elements.
  3. Final Answer:

    heap[i], heap[parent] = heap[parent], heap[i] -> Option A
  4. Quick Check:

    Simultaneous assignment swaps elements [OK]
Hint: Use simultaneous assignment to swap two slice elements [OK]
Common Mistakes:
  • Assigning one element without swapping
  • Using undefined swap function
  • Confusing assignment direction
3. Given the min-heap represented by the slice [2, 5, 3, 10], what will be the heap after inserting 1 and performing bubble up?
medium
A. [2, 5, 3, 10, 1]
B. [1, 2, 3, 10, 5]
C. [1, 5, 2, 10, 3]
D. [1, 2, 3, 5, 10]

Solution

  1. Step 1: Insert 1 at the end

    Heap becomes [2, 5, 3, 10, 1].
  2. Step 2: Bubble up 1

    Compare 1 with parent 5 (index 1), swap: [2, 1, 3, 10, 5]. Then compare 1 with parent 2 (index 0), swap: [1, 2, 3, 10, 5].
  3. Final Answer:

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

    Bubble up swaps until heap property restored [OK]
Hint: Insert at end, then swap up while smaller than parent [OK]
Common Mistakes:
  • Not swapping with grandparent
  • Leaving new element at the end
  • Swapping with wrong parent index
4. Consider this Go code snippet for bubble up in a min-heap:
for i > 0 && heap[i] < heap[(i-1)/2] {
    heap[i], heap[(i-1)/2] = heap[(i-1)/2], heap[i]
    i = (i-1)/2
}
What is the bug in this code?
medium
A. No bug; the code correctly bubbles up the element
B. The loop condition should use <= instead of <
C. The index update should be i = i/2 instead of (i-1)/2
D. The parent index calculation should be (i-1)/2, but it is correct here

Solution

  1. Step 1: Check parent index calculation

    Parent index in a binary heap is (i-1)/2, which is correctly used.
  2. Step 2: Verify loop condition and index update

    Loop continues while current element is less than parent; index updates to parent correctly.
  3. Final Answer:

    No bug; the code correctly bubbles up the element -> Option A
  4. Quick Check:

    Parent index and swaps are correct [OK]
Hint: Parent index = (i-1)/2; loop swaps while child < parent [OK]
Common Mistakes:
  • Miscomputing parent index
  • Using wrong comparison operator
  • Incorrect index update after swap
5. You have a max-heap represented by the slice [90, 70, 80, 40, 50, 60]. You insert 85 at the end and perform bubble up. What is the final heap after bubble up?
hard
A. [90, 85, 80, 40, 50, 60, 70]
B. [85, 70, 90, 40, 50, 60, 80]
C. [90, 70, 80, 40, 50, 60, 85]
D. [90, 70, 85, 40, 50, 60, 80]

Solution

  1. Step 1: Insert 85 at the end

    Heap becomes [90, 70, 80, 40, 50, 60, 85].
  2. Step 2: Bubble up 85 in max-heap

    Parent of index 6 is (6-1)/2 = 2 with value 80. Since 85 > 80, swap: [90, 70, 85, 40, 50, 60, 80]. Now index 2's parent is index 0 (90), 85 < 90, stop.
  3. Final Answer:

    [90, 70, 85, 40, 50, 60, 80] -> Option D
  4. Quick Check:

    Bubble up swaps while child > parent in max-heap [OK]
Hint: In max-heap, bubble up swaps while child > parent [OK]
Common Mistakes:
  • Not swapping with parent when child is larger
  • Stopping bubble up too early
  • Confusing max-heap with min-heap rules
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