0
0
DSA Pythonprogramming~20 mins

Maximum Product Subarray in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Product Subarray Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Product Subarray Calculation
What is the output of the following code that finds the maximum product of a contiguous subarray?
DSA Python
def max_product_subarray(nums):
    max_prod = min_prod = result = nums[0]
    for num in nums[1:]:
        candidates = (num, max_prod * num, min_prod * num)
        max_prod = max(candidates)
        min_prod = min(candidates)
        result = max(result, max_prod)
    return result

print(max_product_subarray([2,3,-2,4]))
A6
B4
C8
D-2
Attempts:
2 left
💡 Hint
Think about how negative numbers affect the product and why we track both max and min products.
Predict Output
intermediate
2:00remaining
Result of Maximum Product Subarray with Negative Numbers
What is the output of this code for the input array containing negative numbers?
DSA Python
def max_product_subarray(nums):
    max_prod = min_prod = result = nums[0]
    for num in nums[1:]:
        candidates = (num, max_prod * num, min_prod * num)
        max_prod = max(candidates)
        min_prod = min(candidates)
        result = max(result, max_prod)
    return result

print(max_product_subarray([-2,0,-1]))
A-2
B-1
C0
D1
Attempts:
2 left
💡 Hint
Remember zero resets the product, so consider subarrays around zero.
🔧 Debug
advanced
2:00remaining
Identify the Error in Maximum Product Subarray Implementation
What error will this code produce when run, and why?
DSA Python
def max_product_subarray(nums):
    max_prod = min_prod = result = 0
    for num in nums:
        candidates = (num, max_prod * num, min_prod * num)
        max_prod = max(candidates)
        min_prod = min(candidates)
        result = max(result, max_prod)
    return result

print(max_product_subarray([-2]))
AThe output is -2
BThe output is 0
CThe output is 24
DThe output is 6
Attempts:
2 left
💡 Hint
Check the initial values of max_prod, min_prod, and result.
🧠 Conceptual
advanced
2:00remaining
Why Track Both Maximum and Minimum Products?
Why does the maximum product subarray algorithm track both the maximum and minimum products at each step?
ABecause the minimum product can become maximum if multiplied by a negative number
BBecause minimum product helps to find the smallest subarray length
CBecause maximum product alone is enough to find the answer
DBecause minimum product tracks the sum of negative numbers
Attempts:
2 left
💡 Hint
Think about how multiplying two negative numbers affects the product.
🚀 Application
expert
3:00remaining
Maximum Product Subarray Length Query
Given the array [1, -2, -3, 0, 7, -8, -2], what is the length of the contiguous subarray with the maximum product?
A4
B5
C2
D3
Attempts:
2 left
💡 Hint
Find the subarray with the maximum product first, then count its length.