Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select the pivot as the last element in Lomuto partition.
DSA C#
int pivot = arr[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the first element as pivot instead of the last.
Using an incorrect index like 'mid' or 'low'.
✗ Incorrect
In Lomuto partition, the pivot is chosen as the last element, which is at index 'high'.
2fill in blank
mediumComplete the code to swap elements in Lomuto partition when arr[j] is less than or equal to pivot.
DSA C#
if (arr[j] <= pivot) { i++; Swap(arr, i, [1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with the pivot index instead of j.
Swapping with i itself, which does nothing.
✗ Incorrect
We swap arr[i] and arr[j] when arr[j] is less than or equal to pivot to move smaller elements to the left.
3fill in blank
hardFix the error in the Hoare partition loop condition to avoid infinite loop.
DSA C#
while (arr[i] [1] pivot) i++; while (arr[j] [2] pivot) j--;
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= causes infinite loops or wrong partitioning.
Swapping the comparison operators between i and j loops.
✗ Incorrect
In Hoare partition, we move i forward while arr[i] is less than pivot, and move j backward while arr[j] is greater than pivot.
4fill in blank
hardFill both blanks to correctly swap and return the partition index in Lomuto partition.
DSA C#
Swap(arr, i + 1, [1]); return [2] + 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low or j instead of i + 1.
Swapping with wrong index instead of high.
✗ Incorrect
After partitioning, swap the element after i with the pivot at high, then return i + 1 as the partition index.
5fill in blank
hardFill all three blanks to correctly implement the Hoare partition swap and return index.
DSA C#
if (i >= j) return [1]; Swap(arr, [2], [3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Returning i instead of j.
Swapping with wrong indices or pivot.
✗ Incorrect
In Hoare partition, if i >= j, return j as partition index; otherwise swap arr[i] and arr[j].
