Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new element at the end of the heap array.
DSA C
heap[size] = [1];
size++;Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' as the value instead of the new element.
Forgetting to assign the new element before incrementing size.
✗ Incorrect
We insert the new element 'value' at the end of the heap array before increasing the size.
2fill in blank
mediumComplete the code to find the parent index of the current node during bubble up.
DSA C
int parent = (index - 1) [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division.
Using addition or subtraction instead of division.
✗ Incorrect
The parent index in a heap is calculated by integer division of (index - 1) by 2.
3fill in blank
hardFix the error in the bubble up condition to compare the current node with its parent.
DSA C
while (index > 0 && heap[index] [1] heap[parent]) { // swap and update index }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which is for min-heap.
Using '==' which does not trigger bubble up.
✗ Incorrect
In a max-heap, bubble up continues while the current node is greater than its parent.
4fill in blank
hardFill both blanks to swap the current node with its parent during bubble up.
DSA C
int temp = heap[[1]]; heap[[2]] = heap[index];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' instead of 'parent' for the first blank.
Using different indices for the two blanks causing incorrect swap.
✗ Incorrect
We store the parent's value in temp and then assign the child's value to the parent's position.
5fill in blank
hardFill all three blanks to complete the bubble up swap and update the index.
DSA C
heap[index] = temp; index = [1]; parent = (index [2] 1) [3] 2; if (index == 0) break;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for parent calculation.
Not updating index to parent after swap.
✗ Incorrect
After swapping, update index to parent, recalculate parent by dividing (index - 1) by 2, and break if index is zero.
