Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index of the array segment.
DSA C#
int mid = (low + [1]) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using low twice instead of high.
Using length instead of high.
Using mid variable before assignment.
✗ Incorrect
The middle index is calculated by adding low and high indices and dividing by 2.
2fill in blank
mediumComplete the code to return the maximum of three values.
DSA C#
return Math.Max(leftMax, Math.Max(rightMax, [1]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not defined in the function.
Using maxSum which is not declared.
Using midMax which is not part of the algorithm.
✗ 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 C#
for (int i = mid; i >= [1]; i--) { sum += arr[i]; if (sum > leftSum) leftSum = sum; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using high as lower bound in left loop.
Using mid as lower bound causing infinite loop.
Using 0 which may be out of current segment.
✗ Incorrect
The loop must go from mid down to low index to cover left half.
4fill in blank
hardFill both blanks to calculate the right crossing sum correctly.
DSA C#
int sum = 0; int rightSum = int.MinValue; for (int j = mid + 1; j [1] [2]; j++) { sum += arr[j]; if (sum > rightSum) rightSum = sum; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the high index.
Using low as upper bound is incorrect.
Using '>' causes loop not to run.
✗ Incorrect
The loop runs from mid+1 up to and including high index. Using '<=' and 'high' is correct.
5fill in blank
hardFill all three blanks to complete the recursive maximum subarray function.
DSA C#
if (low == high) return arr[[1]]; int mid = (low + high) / 2; int leftMax = MaxSubArray(arr, low, [2]); int rightMax = MaxSubArray(arr, [3], high);
Attempts:
3 left
💡 Hint
Common Mistakes
Using high in base case index.
Using mid+1 in left recursive call.
Using mid in right recursive call.
✗ Incorrect
Base case returns arr[low]. Left recursive call is from low to mid. Right recursive call is from mid+1 to high.
