Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the list.
DSA Python
arr[[1]], arr[j] = arr[j], arr[[1]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' instead of 'i' causes swapping the wrong elements.
Using '0' or 'n' causes index errors.
✗ Incorrect
Swapping elements at positions i and j is done by arr[i], arr[j] = arr[j], arr[i].
2fill in blank
mediumComplete the code to find the left child index in the heap.
DSA Python
left = 2 * i [1] 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction results in incorrect child index.
Using multiplication or floor division is incorrect here.
✗ Incorrect
The left child index in a heap is calculated as 2 * i + 1.
3fill in blank
hardFix the error in the heapify function to correctly compare child and parent values.
DSA Python
if left < n and arr[left] [1] arr[largest]: largest = left
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the logic and breaks heap property.
Using '==' or '<=' does not correctly identify the largest.
✗ Incorrect
To maintain max heap property, we check if left child is greater than current largest.
4fill in blank
hardFill both blanks to complete the heapify function's recursive call and condition.
DSA Python
if largest != [1]: arr[[2]], arr[i] = arr[i], arr[[2]] heapify(arr, n, largest)
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with wrong indices breaks heap structure.
Not recursing on largest misses fixing subtree.
✗ Incorrect
We check if largest is not i, then swap arr[largest] with arr[i] and recurse on largest.
5fill in blank
hardFill the two blanks to complete the heap sort main loop that extracts elements from the heap.
DSA Python
for i in range(n-1, [1], -1): arr[0], arr[i] = arr[i], arr[0] heapify(arr, i, [2])
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop range causes index errors.
Heapifying from wrong index breaks heap property.
✗ Incorrect
The loop runs from n-1 down to 1 (exclusive), swapping root with last element, then heapifying from root index 0.
