Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index for dividing the array.
DSA C++
int mid = (left [1] right) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication which gives wrong middle.
Forgetting to divide by 2.
✗ Incorrect
We add left and right indices and divide by 2 to find the middle index.
2fill in blank
mediumComplete the code to recursively call mergeSort on the left half.
DSA C++
mergeSort(arr, left, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using right instead of mid.
Using mid + 1 which is for the right half.
Using left as end index which does not divide the array.
✗ Incorrect
The left half is from left to mid, so we pass mid as the end index.
3fill in blank
hardFix the error in the merge function call to merge two halves.
DSA C++
merge(arr, left, mid, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of right as the last index.
Using mid - 1 which is before mid.
Using left which is the start index.
✗ Incorrect
The merge function merges from left to mid and mid+1 to right, so right is the last index.
4fill in blank
hardFill both blanks to complete the mergeSort base condition and recursive call.
DSA C++
if (left [1] right) return; mergeSort(arr, [2], mid);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in base case which causes infinite recursion.
Using mid + 1 instead of left for left half start.
Confusing base case condition.
✗ Incorrect
Base case stops when left is greater or equal to right. Recursive call on left half uses left as start.
5fill in blank
hardFill all three blanks to complete the mergeSort recursive calls and merge step.
DSA C++
mergeSort(arr, left, [1]); mergeSort(arr, [2], right); merge(arr, left, [3], right);
Attempts:
3 left
💡 Hint
Common Mistakes
Using left instead of mid for left half end.
Using mid instead of mid+1 for right half start.
Using wrong middle index in merge.
✗ Incorrect
Left half ends at mid, right half starts at mid+1, merge uses mid as middle index.
