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 Javascript
function lomutoPartition(arr, low, high) {
let pivot = arr[1];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the first element as pivot instead of the last.
Using an index outside the current subarray.
✗ 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 Javascript
if (arr[j] [1] pivot) { i++; [arr[i], arr[j]] = [arr[j], arr[i]]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only '<' and missing equal elements.
Using '>' which reverses the logic.
✗ Incorrect
We swap when arr[j] is less than or equal to pivot to group smaller or equal elements to the left.
3fill in blank
hardFix the error in the Hoare partition loop condition to correctly partition the array.
DSA Javascript
while (arr[i] [1] pivot) i++; while (arr[j] [2] pivot) j--;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= which can cause infinite loops.
Swapping the comparison directions.
✗ Incorrect
In Hoare partition, we move i forward while arr[i] is less than pivot, and j backward while arr[j] is greater than pivot.
4fill in blank
hardFill both blanks to complete the Hoare partition swap condition and return statement.
DSA Javascript
if (i >= j) return [1]; [arr[i], arr[j]] = [arr[j], arr[i]]; // Return index to split array return [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning i instead of j.
Returning low or high which are boundaries, not partition index.
✗ Incorrect
When i >= j, we return j as the partition index in Hoare partition.
5fill in blank
hardFill all three blanks to complete the Lomuto partition function with correct pivot selection, swap, and return.
DSA Javascript
function lomutoPartition(arr, low, high) {
let pivot = arr[1];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] [2] pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return [3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pivot index.
Using wrong comparison operator.
Returning wrong partition index.
✗ Incorrect
Pivot is last element arr[high], swap when arr[j] <= pivot, and return i+1 as partition index.