Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum product with the first element of the array.
DSA C++
int maxProduct(vector<int>& nums) {
int maxProd = nums[[1]];
return maxProd;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using curly braces which is invalid for indexing.
✗ Incorrect
We access the first element of the vector using square brackets and index 0: nums[0].
2fill in blank
mediumComplete the code to update the maximum product at each step by comparing with the current product.
DSA C++
maxProd = max(maxProd, [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums[i] instead of the current max product.
Using the loop index variable i directly.
✗ Incorrect
We compare maxProd with the current maximum product ending at index i, stored in currMax.
3fill in blank
hardFix the error in updating currMax and currMin when nums[i] is negative.
DSA C++
if (nums[i] < 0) { swap([1], [2]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping wrong variables like maxProd and currMin.
Not swapping at all leading to incorrect results.
✗ Incorrect
When nums[i] is negative, we swap currMax and currMin before updating them to handle sign changes.
4fill in blank
hardFill both blanks to update currMax and currMin correctly after handling negative numbers.
DSA C++
currMax = max(nums[i], currMax [1] nums[i]); currMin = min(nums[i], currMin [2] nums[i]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Using division which is incorrect here.
✗ Incorrect
We multiply currMax and currMin by nums[i] to extend the subarray product or start fresh from nums[i].
5fill in blank
hardFill all three blanks to complete the loop that calculates the maximum product subarray.
DSA C++
for (int i = 1; i [1] nums.size(); i++) { if (nums[i] < 0) { swap([2], [3]); } currMax = max(nums[i], currMax * nums[i]); currMin = min(nums[i], currMin * nums[i]); maxProd = max(maxProd, currMax); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < in the loop condition causing out-of-bounds error.
Swapping wrong variables like maxProd.
Not swapping at all.
✗ Incorrect
The loop runs while i is less than nums.size(). We swap currMax and currMin when nums[i] is negative.
