Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Java
int temp = arr[[1]]; arr[[1]] = arr[j]; arr[j] = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i causes swapping the wrong elements.
Using pivot or low as index causes errors.
✗ Incorrect
We swap the element at index i with the element at index j to move smaller elements before the pivot.
2fill in blank
mediumComplete the code to choose the pivot element in Lomuto partition.
DSA Java
int pivot = arr[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Choosing low or mid as pivot changes partition behavior.
Using 0 as pivot index ignores current subarray boundaries.
✗ Incorrect
In Lomuto partition, the pivot is chosen as the last element at index high.
3fill in blank
hardFix the error in the Hoare partition loop condition to avoid infinite loop.
DSA Java
while (arr[i] [1] pivot) i++; while (arr[j] [2] pivot) j--;
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= in conditions causes infinite loops.
Reversing signs breaks partition logic.
✗ Incorrect
In Hoare partition, i moves forward while arr[i] is less than pivot, and j moves backward while arr[j] is greater than pivot.
4fill in blank
hardFill in the blank to complete the Lomuto partition loop that moves smaller elements before pivot.
DSA Java
for (int j = low; j < high; j++) { if (arr[j] [1] pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '>=' reverses the logic and breaks partition.
Using '<=' may cause different partition behavior but is not the standard Lomuto.
✗ Incorrect
In Lomuto partition, elements less than or equal to pivot are moved before it. Using '<' ensures strict less than comparison.
5fill in blank
hardFill all three blanks to complete the Hoare partition swap and return statement.
DSA Java
if (i >= j) return [1]; int temp = arr[[2]]; arr[[2]] = arr[[3]]; arr[[3]] = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Returning i instead of j causes incorrect partition index.
Swapping wrong indices breaks partition correctness.
✗ Incorrect
When pointers cross, return j as partition index. Swap arr[i] and arr[j] to place elements correctly.
