Challenge - 5 Problems
Maximum Product 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 TypeScript code that finds the maximum product of a contiguous subarray?
DSA Typescript
function maxProduct(nums: number[]): number {
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 how swapping max and min helps.
✗ Incorrect
The maximum product subarray is [2, 3] with product 6. The negative number -2 resets the product, so the max product remains 6.
❓ Predict Output
intermediateResult of Maximum Product Subarray with Negative Numbers
What will be printed after running this TypeScript code?
DSA Typescript
function maxProduct(nums: number[]): number {
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, 0, -1]));Attempts:
2 left
💡 Hint
Zero resets the product, so consider subarrays around zero.
✗ Incorrect
The maximum product subarray is [0] with product 0. Negative numbers alone produce smaller products.
🔧 Debug
advancedIdentify the Error in Maximum Product Subarray Implementation
This TypeScript function aims to find the maximum product subarray but produces wrong results. What is the error?
DSA Typescript
function maxProduct(nums: number[]): number {
let maxProd = 1;
let minProd = 1;
let result = Number.MIN_SAFE_INTEGER;
for (let num of nums) {
let tempMax = maxProd * num;
maxProd = Math.max(tempMax, minProd * num, num);
minProd = Math.min(tempMax, minProd * num, num);
result = Math.max(result, maxProd);
}
return result;
}
console.log(maxProduct([2, 3, -2, 4]));Attempts:
2 left
💡 Hint
Handling negative numbers requires swapping max and min products.
✗ Incorrect
Without swapping maxProd and minProd when num is negative, the algorithm fails to track correct max/min products.
🚀 Application
advancedMaximum Product Subarray in a Circular Array
Given a circular array (the end connects to the start), which approach correctly finds the maximum product subarray?
Attempts:
2 left
💡 Hint
Think about how circular subarrays can wrap around and how to handle that.
✗ Incorrect
The maximum product subarray in a circular array can be the max of normal max product or the product that wraps around, which is total product divided by minimum product subarray.
🧠 Conceptual
expertWhy Track Both Maximum and Minimum Products in Maximum Product Subarray?
Why does the algorithm for maximum product subarray keep track of both maximum and minimum products at each step?
Attempts:
2 left
💡 Hint
Consider how multiplying by a negative number affects product signs.
✗ Incorrect
Multiplying a negative number by the current minimum product (which may be negative) can produce a new maximum product, so both must be tracked.
