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 Go code that finds the maximum product of a contiguous subarray?
DSA Go
package main import "fmt" func maxProduct(nums []int) int { maxProd := nums[0] minProd := nums[0] result := nums[0] for i := 1; i < len(nums); i++ { if nums[i] < 0 { maxProd, minProd = minProd, maxProd } if nums[i] > maxProd*nums[i] { maxProd = nums[i] } else { maxProd = maxProd * nums[i] } if nums[i] < minProd*nums[i] { minProd = nums[i] } else { minProd = minProd * nums[i] } if maxProd > result { result = maxProd } } return result } func main() { nums := []int{2, 3, -2, 4} fmt.Println(maxProduct(nums)) }
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 tracks max and min products to handle negatives correctly.
❓ Predict Output
intermediateResult of Maximum Product Subarray with Negative Numbers
What will be printed by this Go program that calculates the maximum product subarray?
DSA Go
package main import "fmt" func maxProduct(nums []int) int { maxProd := nums[0] minProd := nums[0] result := nums[0] for i := 1; i < len(nums); i++ { if nums[i] < 0 { maxProd, minProd = minProd, maxProd } if nums[i] > maxProd*nums[i] { maxProd = nums[i] } else { maxProd = maxProd * nums[i] } if nums[i] < minProd*nums[i] { minProd = nums[i] } else { minProd = minProd * nums[i] } if maxProd > result { result = maxProd } } return result } func main() { nums := []int{-2, 0, -1} fmt.Println(maxProduct(nums)) }
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 and zero reset the product tracking.
🔧 Debug
advancedIdentify the Runtime Error in Maximum Product Subarray Code
Which option will cause a runtime error when running the maximum product subarray function in Go?
DSA Go
func maxProduct(nums []int) int { maxProd := nums[0] minProd := nums[0] result := nums[0] for i := 1; i <= len(nums); i++ { if nums[i] < 0 { maxProd, minProd = minProd, maxProd } if nums[i] > maxProd*nums[i] { maxProd = nums[i] } else { maxProd = maxProd * nums[i] } if nums[i] < minProd*nums[i] { minProd = nums[i] } else { minProd = minProd * nums[i] } if maxProd > result { result = maxProd } } return result }
Attempts:
2 left
💡 Hint
Check loop boundary conditions carefully.
✗ Incorrect
Using i <= len(nums) causes an index out of range error because valid indices are 0 to len(nums)-1.
🧠 Conceptual
advancedWhy Track Both Max and Min Products in Maximum Product Subarray?
Why does the maximum product subarray algorithm track both maximum and minimum products at each step?
Attempts:
2 left
💡 Hint
Think about how multiplying by a negative number changes signs.
✗ Incorrect
Multiplying by a negative number flips signs, so the minimum product can become the maximum product and vice versa.
🚀 Application
expertMaximum Product Subarray Length with Maximum Product
Given the array [1, -2, -3, 0, 7, -8, -2], what is the length of the contiguous subarray that produces the maximum product?
Attempts:
2 left
💡 Hint
Find the subarray with the maximum product and count its length.
✗ Incorrect
The maximum product is 112 from subarray [7, -8, -2], which has length 3.
