Challenge - 5 Problems
Maximum Product Subarray Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Product Subarray Calculation
What is the output of the following JavaScript code that finds the maximum product of a contiguous subarray?
DSA Javascript
function maxProduct(nums) {
let maxProd = nums[0];
let minProd = nums[0];
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
[maxProd, minProd] = [minProd, maxProd];
}
maxProd = Math.max(nums[i], maxProd * nums[i]);
minProd = Math.min(nums[i], minProd * nums[i]);
result = Math.max(result, maxProd);
}
return result;
}
console.log(maxProduct([2,3,-2,4]));Attempts:
2 left
💡 Hint
Think about how negative numbers affect the product and why swapping max and min helps.
✗ Incorrect
The maximum product subarray is [2,3] with product 6. The negative number -2 resets the product, but the max product remains 6.
❓ Predict Output
intermediateResult of Maximum Product Subarray with Zeroes
What is the output of this code for the input array containing zeroes?
DSA Javascript
function maxProduct(nums) {
let maxProd = nums[0];
let minProd = nums[0];
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
[maxProd, minProd] = [minProd, maxProd];
}
maxProd = Math.max(nums[i], maxProd * nums[i]);
minProd = Math.min(nums[i], minProd * nums[i]);
result = Math.max(result, maxProd);
}
return result;
}
console.log(maxProduct([0, -2, 0, -1, 0]));Attempts:
2 left
💡 Hint
Zero resets the product, so consider subarrays around zero.
✗ Incorrect
The maximum product subarray is [0] or any zero element, product is 0.
🔧 Debug
advancedIdentify the Error in Maximum Product Subarray Implementation
What error will this code produce when run, and why?
DSA Javascript
function maxProduct(nums) {
let maxProd = 1;
let minProd = 1;
let result = -Infinity;
for (let i = 0; i < nums.length; i++) {
if (nums[i] < 0) {
[maxProd, minProd] = [minProd, maxProd];
}
maxProd = Math.max(nums[i], maxProd * nums[i]);
minProd = Math.min(nums[i], minProd * nums[i]);
result = Math.max(result, maxProd);
}
return result;
}
console.log(maxProduct([2,3,-2,4]));Attempts:
2 left
💡 Hint
Check initial values of maxProd and minProd and how they affect the first iteration.
✗ Incorrect
Initializing maxProd and minProd to 1 causes incorrect multiplication on first element, leading to wrong max product 12 instead of 6.
🧠 Conceptual
advancedWhy Swap maxProd and minProd When Encountering Negative Number?
In the maximum product subarray algorithm, why do we swap maxProd and minProd when the current number is negative?
Attempts:
2 left
💡 Hint
Think about how a negative number changes the sign of the product.
✗ Incorrect
Multiplying by a negative number turns the largest product into the smallest and vice versa, so swapping maxProd and minProd keeps track of correct values.
❓ Predict Output
expertMaximum Product Subarray Output with Mixed Signs and Zeroes
What is the output of this code for the input array with mixed positive, negative numbers and zeroes?
DSA Javascript
function maxProduct(nums) {
let maxProd = nums[0];
let minProd = nums[0];
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
[maxProd, minProd] = [minProd, maxProd];
}
maxProd = Math.max(nums[i], maxProd * nums[i]);
minProd = Math.min(nums[i], minProd * nums[i]);
result = Math.max(result, maxProd);
}
return result;
}
console.log(maxProduct([2, -5, -2, -4, 3]));Attempts:
2 left
💡 Hint
Consider how multiple negatives multiply to positive and track max product carefully.
✗ Incorrect
The maximum product subarray is [-5, -2, -4, 3] with product 24.
