Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the variable that stores the maximum sum found so far.
DSA Java
int maxSoFar = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxSoFar to 0 causes errors when all numbers are negative.
Using Integer.MAX_VALUE will prevent updates to maxSoFar.
✗ Incorrect
We initialize maxSoFar to Integer.MIN_VALUE to ensure any subarray sum will be larger initially.
2fill in blank
mediumComplete the code to update the current subarray sum by adding the current element.
DSA Java
currentSum = currentSum [1] nums[i];Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication changes the logic incorrectly.
Division is not applicable here.
✗ Incorrect
We add the current element to currentSum to extend the subarray.
3fill in blank
hardFix the error in the condition to reset currentSum when it becomes negative.
DSA Java
if (currentSum [1] 0) { currentSum = 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= will reset incorrectly when sum is positive.
Using == only resets when sum is exactly zero, missing negatives.
✗ Incorrect
We reset currentSum to 0 when it is less than 0 to start a new subarray.
4fill in blank
hardComplete the code to update maxSoFar with the larger of maxSoFar and currentSum.
DSA Java
maxSoFar = Math.[1](maxSoFar, currentSum);Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min will select the smaller value incorrectly.
Missing the closing parenthesis causes syntax errors.
✗ Incorrect
Use Math.max to get the larger value and close the method call with a parenthesis.
5fill in blank
hardFill all three blanks to complete the for-loop iterating over the array.
DSA Java
for (int [1] = 0; [2] < nums[3]; [1]++) { // loop body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'n' instead of 'i' inconsistently.
Using nums[i] in the condition instead of nums.length.
Forgetting to increment the loop variable.
✗ Incorrect
Use 'i' as the loop variable, compare i with nums.length, and access elements with nums[i].
