Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum product variable.
DSA Typescript
let maxProduct = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxProduct to 0 ignores negative numbers.
Using 1 ignores the actual first element which might be negative or zero.
✗ Incorrect
We start with the first element as the initial maximum product because the product subarray must include at least one element.
2fill in blank
mediumComplete the code to update the current maximum product at each step.
DSA Typescript
maxEndingHere = Math.max(nums[i], nums[i] * [1], nums[i] * minEndingHere);
Attempts:
3 left
💡 Hint
Common Mistakes
Using minEndingHere instead of maxEndingHere causes wrong max calculation.
Using maxProduct instead of maxEndingHere mixes global max with local max.
✗ Incorrect
maxEndingHere is updated by comparing the current number, the product with previous max, and the product with previous min (to handle negatives).
3fill in blank
hardFix the error in swapping maxEndingHere and minEndingHere when nums[i] is negative.
DSA Typescript
if (nums[i] < 0) { let temp = [1]; [2] = maxEndingHere; maxEndingHere = temp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with wrong variables causes incorrect product calculation.
Not swapping at all misses negative number effect.
✗ Incorrect
When the current number is negative, max and min products swap because multiplying by negative flips signs.
4fill in blank
hardFill both blanks to update minEndingHere and maxProduct after processing nums[i].
DSA Typescript
minEndingHere = Math.min(nums[i], nums[i] * [1]); maxProduct = Math.max(maxProduct, [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxEndingHere to update minEndingHere causes wrong min calculation.
Updating maxProduct with minEndingHere causes wrong global max.
✗ Incorrect
minEndingHere updates with the minimum product ending at i; maxProduct updates with the global maximum product found so far.
5fill in blank
hardFill all three blanks to complete the loop for maximum product subarray.
DSA Typescript
for (let i = 1; i < nums.length; i++) { if (nums[i] < 0) { let temp = [1]; [2] = maxEndingHere; maxEndingHere = temp; } maxEndingHere = Math.max(nums[i], nums[i] * maxEndingHere, nums[i] * minEndingHere); minEndingHere = Math.min(nums[i], nums[i] * [3]); maxProduct = Math.max(maxProduct, maxEndingHere); }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping wrong variables or forgetting to swap.
Using maxEndingHere instead of minEndingHere in min calculation.
✗ Incorrect
Swapping minEndingHere and maxEndingHere handles negatives; minEndingHere updates with previous min; maxProduct tracks global max.
