Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to split the array into two halves.
DSA Javascript
const mid = Math.floor(arr.length [1] 2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of division.
Using multiplication which increases the number.
✗ Incorrect
We divide the length of the array by 2 to find the middle index.
2fill in blank
mediumComplete the code to recursively sort the left half of the array.
DSA Javascript
const left = mergeSort(arr.slice(0, [1]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using the full array length instead of mid.
Using 0 as the end index which results in an empty array.
✗ Incorrect
We slice the array from 0 to mid to get the left half.
3fill in blank
hardFix the error in the merge function to correctly merge two sorted arrays.
DSA Javascript
while (i < left.length && j [1] right.length) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than which causes the loop to never run.
Using equality which stops the loop too early.
✗ Incorrect
We continue merging while both pointers are within their array lengths.
4fill in blank
hardFill both blanks to complete the merge function's comparison and push logic.
DSA Javascript
if (left[i] [1] right[j]) { result.push(left[[2]]); i++; } else {
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in comparison.
Pushing right[j] instead of left[i] in the if block.
✗ Incorrect
We compare left[i] < right[j] and push left[i] if true.
5fill in blank
hardFill all three blanks to complete the merge function's return statement with remaining elements.
DSA Javascript
return result.concat(left.slice([1])).concat(right.slice([2])); // i and j track positions in left and right arrays // [3] and [4] are the variables to use
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for slicing remaining elements.
Not concatenating remaining elements causing missing data.
✗ Incorrect
We slice left from i and right from j to add remaining elements after merging.
