Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the midpoint of the array segment.
DSA Python
def find_max_subarray(arr, low, high): if low == high: return arr[low] mid = (low + high) [1] 2 # further code omitted
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of division causes wrong midpoint.
Using '*' or '-' does not find midpoint.
✗ Incorrect
We use integer division '//' to get the midpoint index between low and high.
2fill in blank
mediumComplete the code to find the maximum crossing subarray sum.
DSA Python
def max_crossing_sum(arr, low, mid, high): left_sum = float('-inf') sum = 0 for i in range(mid, low - 1, -1): sum += arr[i] if sum [1] left_sum: left_sum = sum # further code omitted
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes no update to left_sum.
Using '==' misses larger sums.
✗ Incorrect
We update left_sum when current sum is greater than left_sum.
3fill in blank
hardFix the error in the recursive call to find maximum subarray sum.
DSA Python
def find_max_subarray(arr, low, high): if low == high: return arr[low] mid = (low + high) // 2 left_sum = find_max_subarray(arr, low, [1]) right_sum = find_max_subarray(arr, mid + 1, high) # further code omitted
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'high' causes overlap with right half.
Using 'mid + 1' in left call causes empty segment.
✗ Incorrect
The left recursive call should cover from low to mid.
4fill in blank
hardFill both blanks to return the maximum of three sums.
DSA Python
def find_max_subarray(arr, low, high): # code omitted return max([1], [2], max_crossing_sum(arr, low, mid, high))
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mid' or 'cross_sum' instead of left_sum or right_sum.
Not including all three sums in max function.
✗ Incorrect
We compare left_sum, right_sum, and crossing sum to find the max.
5fill in blank
hardFill all three blanks to complete the max_crossing_sum function.
DSA Python
def max_crossing_sum(arr, low, mid, high): left_sum = float('-inf') sum = 0 for i in range(mid, [1], -1): sum += arr[i] if sum > left_sum: left_sum = sum right_sum = float('-inf') sum = 0 for j in range(mid + 1, [2]): sum += arr[j] if sum > right_sum: right_sum = sum return left_sum + right_sum # Call example: max_crossing_sum(arr, 0, 3, [3])
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'low' instead of 'low - 1' causes missing first element.
Using 'high' instead of 'high + 1' causes missing last element.
Passing wrong high index in call.
✗ Incorrect
The left loop goes down to low - 1 (exclusive), right loop goes up to high + 1 (exclusive), and high is the last index.
