Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index of the array segment.
DSA Javascript
const mid = Math.floor((left + [1]) / 2);
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 Javascript
return Math.max(leftSum, rightSum, [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using midSum which is undefined.
Using totalSum which is not calculated.
✗ 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 the left crossing sum.
DSA Javascript
for (let i = mid; i >= [1]; i--) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of left index causes wrong range.
Using right index causes loop to never run.
✗ Incorrect
The loop should go from mid down to left boundary.
4fill in blank
hardFill both blanks to calculate the right crossing sum correctly.
DSA Javascript
for (let j = mid + 1; j <= [1]; j++) { sum += arr[j]; rightSum = Math.max(rightSum, [2]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using left instead of right for loop end.
Updating rightSum with arr[j] instead of sum.
✗ Incorrect
The loop runs to right boundary and updates rightSum with current sum.
5fill in blank
hardFill all three blanks to complete the divide and conquer function.
DSA Javascript
function maxSubArray(arr, [1], [2]) { if ([1] === [2]) return arr[[1]]; const mid = Math.floor(([1] + [2]) / 2); const leftSum = maxSubArray(arr, [1], mid); const rightSum = maxSubArray(arr, mid + 1, [2]); const crossSum = maxCrossingSum(arr, [1], mid, [2]); return Math.max(leftSum, rightSum, crossSum); }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right parameters.
Using mid instead of left or right in base case.
✗ Incorrect
The function uses left and right indices to divide the array and find max sums.
