Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to split the array into two halves.
DSA Typescript
const mid = Math.floor(arr.length [1] 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of division.
Using multiplication which increases the length.
✗ Incorrect
We divide the length by 2 to find the middle index for splitting.
2fill in blank
mediumComplete the code to recursively sort the left half of the array.
DSA Typescript
const left = divideAndConquer(arr.slice(0, [1]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the full length instead of mid.
Starting slice from mid instead of 0.
✗ Incorrect
We slice the array from 0 to mid to get the left half for recursion.
3fill in blank
hardFix the error in the merge function to combine two sorted arrays.
DSA Typescript
while (i < left.length && j [1] right.length) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than which causes skipping the loop.
Using equality which stops early.
✗ Incorrect
We continue merging while both indices are within their array lengths.
4fill in blank
hardFill in the blank to compare and merge elements correctly.
DSA Typescript
if (left[i] [1] right[j]) { result.push(left[i]); i++; } else { result.push(right[j]); j++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality which does not sort properly.
Using greater than in the wrong place.
✗ Incorrect
We push the smaller element first; if left[i] is less, push it, else push right[j].
5fill in blank
hardFill all three blanks to complete the divide and conquer merge sort function.
DSA Typescript
function mergeSort(arr: number[]): number[] {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length [1] 2);
const left = mergeSort(arr.slice(0, [2]));
const right = mergeSort(arr.slice([3]));
return merge(left, right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of division for mid.
Incorrect slice indices causing wrong splits.
✗ Incorrect
Divide length by 2 for mid, slice left from 0 to mid, right from mid to end.