Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum product with the first element of the array.
DSA Go
maxProduct := nums[1]Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using curly braces which are invalid for indexing.
✗ Incorrect
We use nums[0] to get the first element of the slice in Go.
2fill in blank
mediumComplete the code to update the maximum product if the current product is greater.
DSA Go
if currMax > [1] { maxProduct = currMax }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing currMax with currMin instead of maxProduct.
Using an undefined variable like prevMax.
✗ Incorrect
We compare currMax with maxProduct to update the maximum product found so far.
3fill in blank
hardFix the error in swapping currMax and currMin when nums[i] is negative.
DSA Go
if nums[i] < 0 { [1] = currMax currMax = currMin currMin = temp }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping without using a temporary variable causes incorrect values.
Assigning currMin to temp instead of currMax.
✗ Incorrect
We need to store currMax in a temporary variable before swapping.
4fill in blank
hardFill both blanks to update currMax and currMin with the maximum and minimum of nums[i] and their products.
DSA Go
currMax = max(nums[i], currMax[1]nums[i]) currMin = min(nums[i], currMin[2]nums[i])
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Using division which is incorrect here.
✗ Incorrect
We multiply nums[i] with currMax and currMin to update the products.
5fill in blank
hardFill all three blanks to complete the function that returns the maximum product subarray.
DSA Go
func maxProduct(nums []int) int {
maxProduct := nums[1]
currMax, currMin := nums[0], nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] < 0 {
[2] = currMax
currMax = currMin
currMin = [3]
}
currMax = max(nums[i], currMax*nums[i])
currMin = min(nums[i], currMin*nums[i])
if currMax > maxProduct {
maxProduct = currMax
}
}
return maxProduct
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Not using a temporary variable for swapping.
✗ Incorrect
We initialize maxProduct with nums[0], use temp to hold currMax before swapping, and assign currMin to temp during swap.
