Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the pointer for the next position of 0.
DSA Java
int low = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting 'low' at 1 or -1 causes index errors.
Initializing 'low' at nums.length is out of bounds.
✗ Incorrect
The 'low' pointer starts at index 0 to track where the next 0 should be placed.
2fill in blank
mediumComplete the code to swap the current element with the element at the 'low' pointer.
DSA Java
int temp = nums[i]; nums[i] = nums[[1]]; nums[low] = temp;Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'high' pointer moves wrong elements.
Swapping with 'i' does nothing.
Using 0 as index causes wrong swaps.
✗ Incorrect
We swap nums[i] with nums[low] to move 0s to the front.
3fill in blank
hardFix the error in the loop condition to correctly iterate through the array.
DSA Java
while (i [1] high) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' can cause index out of bounds if 'high' is the last index.
Using '>' or '>=' causes infinite loops or no iteration.
✗ Incorrect
We use '<' to iterate until just before 'high' index, since 'high' is inclusive in the algorithm.
4fill in blank
hardFill both blanks to correctly update pointers after swapping 0 and 2.
DSA Java
if (nums[i] == 0) { swap(nums, i, [1]); [2]++; i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing 'high' instead of 'low' after swapping 0.
Not incrementing 'i' causes infinite loop.
✗ Incorrect
After swapping with 'low', increment 'low' and 'i' to move forward.
5fill in blank
hardFill all three blanks to correctly handle swapping 2 and updating pointers.
DSA Java
if (nums[i] == 2) { swap(nums, i, [1]); [2]--; [3] = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing 'i' after swapping 2 causes skipping elements.
Swapping with 'low' instead of 'high' is incorrect.
✗ Incorrect
Swap with 'high', decrement 'high', and keep 'i' same to recheck swapped element.
