Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to split the array into two halves.
DSA Java
int mid = arr.length [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '/' causes incorrect middle index.
Using '*' or '-' will not split the array correctly.
✗ Incorrect
We divide the length by 2 to find the middle index for splitting the array.
2fill in blank
mediumComplete the code to copy the left half of the array.
DSA Java
int[] left = Arrays.copyOfRange(arr, 0, [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr.length copies the whole array, not just the left half.
Using 0 as end index results in an empty array.
✗ Incorrect
We copy from index 0 to mid to get the left half of the array.
3fill in blank
hardFix the error in the merge function call to correctly merge two sorted halves.
DSA Java
merge(arr, left, right, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Passing left.length or right.length causes incomplete merging.
Passing 0 causes no merging to happen.
✗ Incorrect
The merge function needs the total length of the array to merge all elements.
4fill in blank
hardFill both blanks to complete the recursive calls for merge sort.
DSA Java
mergeSort([1]); mergeSort([2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mergeSort on arr twice causes infinite recursion.
Using mid as argument is incorrect since it's an index, not an array.
✗ Incorrect
We recursively sort the left and right halves separately.
5fill in blank
hardFill all three blanks to complete the merge function's main loop conditions.
DSA Java
while (i < [1] && j < [2]) { if (left[i] <= right[j]) { arr[k++] = left[i++]; } else { arr[k++] = right[j++]; } } while (i < [3]) { arr[k++] = left[i++]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr.length in the first while causes index errors.
Using k as a bound is incorrect since k is the merged array index.
✗ Incorrect
We compare elements while i and j are within left and right lengths, then copy remaining left elements.
