Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index of the array segment.
DSA Typescript
const mid = Math.floor((left + [1]) / 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using left twice instead of left and right.
Using array length instead of right index.
✗ Incorrect
The middle index is calculated by averaging left and right indices.
2fill in blank
mediumComplete the code to return the maximum of three values.
DSA Typescript
return Math.max(leftSum, rightSum, [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not representing crossing sum.
Forgetting to include crossing sum in max calculation.
✗ Incorrect
The maximum subarray sum is the max of left, right, and crossing sums.
3fill in blank
hardFix the error in the loop that calculates left crossing sum.
DSA Typescript
for (let i = mid; i >= [1]; i--) { leftSum += arr[i]; maxLeft = Math.max(maxLeft, leftSum); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of left index.
Using mid or right incorrectly as loop boundary.
✗ Incorrect
The loop must go down to the left boundary index.
4fill in blank
hardFill both blanks to calculate right crossing sum correctly.
DSA Typescript
let rightSum = 0; let maxRight = -Infinity; for (let j = mid + 1; j [1] [2]; j++) { rightSum += arr[j]; maxRight = Math.max(maxRight, rightSum); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= causing missing last element.
Using left instead of right as boundary.
✗ Incorrect
The loop runs from mid+1 up to right index (inclusive).
5fill in blank
hardFill all three blanks to complete the recursive maximum subarray function.
DSA Typescript
function maxSubArray(arr: number[], left: number, right: number): number { if (left === right) return [1]; const mid = Math.floor((left + right) / 2); const leftSum = maxSubArray(arr, left, [2]); const rightSum = maxSubArray(arr, [3], right); const crossSum = maxCrossingSum(arr, left, mid, right); return Math.max(leftSum, rightSum, crossSum); } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning arr[right] in base case.
Using wrong indices in recursive calls.
✗ Incorrect
Base case returns single element; recursive calls split at mid and mid+1.