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 C# code that finds the maximum product of a contiguous subarray?
DSA C#
int[] nums = {2, 3, -2, 4}; int maxProduct = nums[0], minProduct = nums[0], result = nums[0]; for (int i = 1; i < nums.Length; i++) { int tempMax = maxProduct; maxProduct = Math.Max(nums[i], Math.Max(maxProduct * nums[i], minProduct * nums[i])); minProduct = Math.Min(nums[i], Math.Min(tempMax * nums[i], minProduct * nums[i])); result = Math.Max(result, maxProduct); } Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Track max and min products at each step because negative numbers can flip signs.
✗ Incorrect
The maximum product subarray is [2,3] with product 6. The code keeps track of max and min products to handle negatives.
❓ Predict Output
intermediateResult of Maximum Product Subarray with Negative Numbers
What will be printed by this C# code for the maximum product subarray?
DSA C#
int[] nums = {-2, 0, -1}; int maxProduct = nums[0], minProduct = nums[0], result = nums[0]; for (int i = 1; i < nums.Length; i++) { int tempMax = maxProduct; maxProduct = Math.Max(nums[i], Math.Max(maxProduct * nums[i], minProduct * nums[i])); minProduct = Math.Min(nums[i], Math.Min(tempMax * nums[i], minProduct * nums[i])); result = Math.Max(result, maxProduct); } Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Zero resets the product, so consider subarrays starting after zero.
✗ Incorrect
The maximum product subarray is [0] with product 0. Negative numbers and zero reset the product.
🔧 Debug
advancedIdentify the Error in Maximum Product Subarray Implementation
What error will this C# code produce when trying to find the maximum product subarray?
DSA C#
int[] nums = {1, -2, -3, 0, 7, -8, -2}; int maxProduct = 0, minProduct = 0, result = 0; for (int i = 0; i < nums.Length; i++) { int tempMax = maxProduct; maxProduct = Math.Max(nums[i], Math.Max(maxProduct * nums[i], minProduct * nums[i])); minProduct = Math.Min(nums[i], Math.Min(tempMax * nums[i], minProduct * nums[i])); result = Math.Max(result, maxProduct); } Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Check initial values of maxProduct and minProduct before loop.
✗ Incorrect
Initializing maxProduct and minProduct to 0 causes incorrect calculations for negative numbers. The code still runs and outputs 112, which is the correct maximum product.
❓ Predict Output
advancedMaximum Product Subarray Output with All Negative Numbers
What is the output of this C# code for maximum product subarray when all numbers are negative?
DSA C#
int[] nums = {-1, -3, -10, -2}; int maxProduct = nums[0], minProduct = nums[0], result = nums[0]; for (int i = 1; i < nums.Length; i++) { int tempMax = maxProduct; maxProduct = Math.Max(nums[i], Math.Max(maxProduct * nums[i], minProduct * nums[i])); minProduct = Math.Min(nums[i], Math.Min(tempMax * nums[i], minProduct * nums[i])); result = Math.Max(result, maxProduct); } Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Product of two negatives is positive, consider subarrays with even count of negatives.
✗ Incorrect
The maximum product subarray is [-3, -10, -2] with product 60.
🧠 Conceptual
expertWhy Track Both Max and Min Products in Maximum Product Subarray?
Why does the maximum product subarray algorithm track both the maximum and minimum products at each step?
Attempts:
2 left
💡 Hint
Think about how negative numbers affect multiplication.
✗ Incorrect
Multiplying a negative minimum product by a negative number can produce a new maximum product, so tracking both is essential.
