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 Java code that finds the maximum product of a contiguous subarray?
DSA Java
public class Solution { public static int maxProduct(int[] nums) { int maxProd = nums[0]; int minProd = nums[0]; int result = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] < 0) { int temp = maxProd; maxProd = minProd; minProd = temp; } maxProd = Math.max(nums[i], maxProd * nums[i]); minProd = Math.min(nums[i], minProd * nums[i]); result = Math.max(result, maxProd); } return result; } public static void main(String[] args) { int[] nums = {2, 3, -2, 4}; System.out.println(maxProduct(nums)); } }
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 code tracks max and min products to handle negatives.
❓ Predict Output
intermediateMaximum Product Subarray Output with Negative Numbers
What is the output of this Java code for the maximum product subarray?
DSA Java
public class Solution { public static int maxProduct(int[] nums) { int maxProd = nums[0]; int minProd = nums[0]; int result = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] < 0) { int temp = maxProd; maxProd = minProd; minProd = temp; } maxProd = Math.max(nums[i], maxProd * nums[i]); minProd = Math.min(nums[i], minProd * nums[i]); result = Math.max(result, maxProd); } return result; } public static void main(String[] args) { int[] nums = {-2, 0, -1}; System.out.println(maxProduct(nums)); } }
Attempts:
2 left
💡 Hint
Zero resets the product, consider subarrays around zero.
✗ Incorrect
The maximum product subarray is [0] with product 0. Negative numbers and zero reset the product.
🧠 Conceptual
advancedWhy Swap maxProd and minProd When Encountering Negative Number?
In the maximum product subarray algorithm, why do we swap the maxProd and minProd variables when the current number is negative?
Attempts:
2 left
💡 Hint
Think about how multiplying by a negative flips signs.
✗ Incorrect
Multiplying by a negative flips the sign, so the max product could become min and vice versa. Swapping helps track this.
❓ Predict Output
advancedOutput of Maximum Product Subarray with All Negative Numbers
What is the output of this Java code for the maximum product subarray when all numbers are negative?
DSA Java
public class Solution { public static int maxProduct(int[] nums) { int maxProd = nums[0]; int minProd = nums[0]; int result = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] < 0) { int temp = maxProd; maxProd = minProd; minProd = temp; } maxProd = Math.max(nums[i], maxProd * nums[i]); minProd = Math.min(nums[i], minProd * nums[i]); result = Math.max(result, maxProd); } return result; } public static void main(String[] args) { int[] nums = {-2, -3, -7}; System.out.println(maxProduct(nums)); } }
Attempts:
2 left
💡 Hint
Consider that multiplying two negatives makes a positive.
✗ Incorrect
The maximum product subarray is [-3, -7] with product 21.
🔧 Debug
expertIdentify the Error in Maximum Product Subarray Implementation
What error will this Java code produce when run?
DSA Java
public class Solution { public static int maxProduct(int[] nums) { int maxProd = 1; int minProd = 1; int result = Integer.MIN_VALUE; for (int i = 0; i < nums.length; i++) { if (nums[i] < 0) { int temp = maxProd; maxProd = minProd; minProd = temp; } maxProd = Math.max(nums[i], maxProd * nums[i]); minProd = Math.min(nums[i], minProd * nums[i]); result = Math.max(result, maxProd); } return result; } public static void main(String[] args) { int[] nums = {2, 3, -2, 4}; System.out.println(maxProduct(nums)); } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop runs with i <= nums.length, which accesses an invalid index causing ArrayIndexOutOfBoundsException.
