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
Swapping with the pivot index instead of the smaller element index.
Using the same index for both swap positions.
✗ 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 the partition method.
DSA Java
int pivot = arr[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the pivot as the first element instead of the last.
Using a fixed index like 0 regardless of sub-array.
✗ Incorrect
The pivot is chosen as the last element in the current array segment, which is at index high.
3fill in blank
hardFix the error in the recursive quickSort call to sort the left partition.
DSA Java
quickSort(arr, low, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using pivotIndex instead of pivotIndex - 1 causes infinite recursion.
Using pivotIndex + 1 sorts the right partition instead.
✗ Incorrect
The left partition ends just before the pivot index, so we use pivotIndex - 1.
4fill in blank
hardFill both blanks to complete the partition loop condition and increment.
DSA Java
for (int j = low; j [1] high; j[2]) { if (arr[j] <= pivot) { i++; // swap code here } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes the pivot to be included in the loop.
Using j-- causes an infinite loop.
✗ Incorrect
The loop runs while j is less than high, and j increments by 1 each time.
5fill in blank
hardFill all three blanks to complete the quickSort recursive calls and base case check.
DSA Java
if (low [1] high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, [2]); quickSort(arr, [3], high); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in base case causes unnecessary recursion.
Swapping pivotIndex - 1 and pivotIndex + 1 in recursive calls.
✗ Incorrect
The base case is when low is less than high. Then sort left partition low to pivotIndex - 1 and right partition pivotIndex + 1 to high.
