Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the index of the left child in a heap array.
DSA C++
int leftChildIndex(int parentIndex) {
return 2 * parentIndex [1] 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using division or multiplication incorrectly.
✗ Incorrect
The left child index in a heap is calculated as 2 * parentIndex + 1.
2fill in blank
mediumComplete the code to swap two elements in the heap array.
DSA C++
void swap(int &a, int &b) {
int temp = a;
a = [1];
b = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'a' to itself.
Using the wrong variable in assignment.
✗ Incorrect
To swap, assign 'a' the value of 'b'.
3fill in blank
hardFix the error in the bubble down condition to check if left child exists.
DSA C++
while (leftChild < heapSize && heap[leftChild] [1] heap[index]) { // bubble down logic }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which is for max-heap.
Using equality or inequality operators incorrectly.
✗ Incorrect
For a min-heap, bubble down if child is less than parent.
4fill in blank
hardFill both blanks to correctly find the smaller child index during bubble down.
DSA C++
int smallerChild = (rightChild < heapSize && heap[rightChild] [1] heap[leftChild]) ? [2] : leftChild;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' in comparison.
Assigning leftChild instead of rightChild when right is smaller.
✗ Incorrect
Compare right and left child; choose rightChild if smaller.
5fill in blank
hardFill all three blanks to complete the bubble down step in extract min operation.
DSA C++
if (heap[index] [1] heap[smallerChild]) { swap(heap[index], heap[[2]]); index = [3]; } else { break; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in comparison.
Swapping with wrong index.
Not updating index correctly.
✗ Incorrect
If parent is greater than smaller child, swap and update index to smallerChild.