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 Javascript
let maxProduct = nums[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using nums0 instead of nums[0]
Initializing maxProduct to 0 or 1 which may be incorrect if array has negative numbers
✗ Incorrect
We need to start with the first element of the array, which is nums[0].
2fill in blank
mediumComplete the code to loop through the array starting from the second element.
DSA Javascript
for (let i = [1]; i < nums.length; i++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 which repeats the first element
Using nums[0] as loop start which is invalid
✗ Incorrect
We start looping from index 1 because index 0 is already used to initialize maxProduct.
3fill in blank
hardFix the error in updating the current maximum product when the current number is negative.
DSA Javascript
if (nums[i] < 0) { [maxEndingHere, minEndingHere] = [[1], [2]]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Not swapping the variables
Swapping incorrectly by assigning in wrong order
✗ Incorrect
When the current number is negative, we swap maxEndingHere and minEndingHere to handle sign change.
4fill in blank
hardFill both blanks to update maxEndingHere and minEndingHere correctly.
DSA Javascript
maxEndingHere = Math.max(nums[i], nums[i] [1] maxEndingHere, nums[i] [2] minEndingHere);
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of *
Using / which is division and incorrect here
✗ Incorrect
We multiply nums[i] with maxEndingHere and minEndingHere to find new max product candidates.
5fill in blank
hardFill all three blanks to update minEndingHere and maxProduct correctly.
DSA Javascript
minEndingHere = Math.min(nums[i], nums[i] [1] minEndingHere, nums[i] [2] maxEndingHere); maxProduct = Math.max(maxProduct, [3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of *
Updating maxProduct with minEndingHere instead of maxEndingHere
✗ Incorrect
We multiply nums[i] with minEndingHere and maxEndingHere to find new min product candidates, and update maxProduct with maxEndingHere.
