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 Rust code that finds the maximum product of a contiguous subarray?
DSA Rust
use std::vec::Vec;
fn max_product(nums: Vec<i32>) -> i32 {
let mut max_prod = nums[0];
let mut min_prod = nums[0];
let mut result = nums[0];
for &num in nums.iter().skip(1) {
let cand1 = max_prod * num;
let cand2 = min_prod * num;
max_prod = num.max(cand1).max(cand2);
min_prod = num.min(cand1).min(cand2);
result = result.max(max_prod);
}
result
}
fn main() {
let nums = vec![2, 3, -2, 4];
println!("{}", max_product(nums));
}Attempts:
2 left
💡 Hint
Think about the subarray that gives the maximum product, considering 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 negative numbers.
❓ Predict Output
intermediateResult of Maximum Product Subarray with Negative Numbers
What will be printed by this Rust program that calculates the maximum product subarray?
DSA Rust
use std::vec::Vec;
fn max_product(nums: Vec<i32>) -> i32 {
let mut max_prod = nums[0];
let mut min_prod = nums[0];
let mut result = nums[0];
for &num in nums.iter().skip(1) {
let cand1 = max_prod * num;
let cand2 = min_prod * num;
max_prod = num.max(cand1).max(cand2);
min_prod = num.min(cand1).min(cand2);
result = result.max(max_prod);
}
result
}
fn main() {
let nums = vec![-2, 0, -1];
println!("{}", max_product(nums));
}Attempts:
2 left
💡 Hint
Remember zero resets the product chain.
✗ Incorrect
The maximum product subarray is [0] with product 0.
Negative numbers and zero reset the product tracking.
🔧 Debug
advancedIdentify the Logical Error in Maximum Product Subarray Code
This Rust code aims to find the maximum product subarray but produces wrong results. What is the logical error?
DSA Rust
use std::vec::Vec;
fn max_product(nums: Vec<i32>) -> i32 {
let mut max_prod = 1;
let mut min_prod = 1;
let mut result = i32::MIN;
for &num in nums.iter() {
if num == 0 {
max_prod = 1;
min_prod = 1;
continue;
}
let temp = max_prod * num;
max_prod = max_prod.max(min_prod) * num;
min_prod = min_prod.min(temp);
result = result.max(max_prod);
}
result
}Attempts:
2 left
💡 Hint
Check how max_prod and min_prod are updated inside the loop.
✗ Incorrect
The code updates max_prod and min_prod incorrectly by multiplying before choosing max/min.
This causes wrong tracking of products.
❓ Predict Output
advancedOutput of Maximum Product Subarray with All Negative Numbers
What is the output of this Rust program that calculates the maximum product subarray for all negative numbers?
DSA Rust
use std::vec::Vec;
fn max_product(nums: Vec<i32>) -> i32 {
let mut max_prod = nums[0];
let mut min_prod = nums[0];
let mut result = nums[0];
for &num in nums.iter().skip(1) {
let cand1 = max_prod * num;
let cand2 = min_prod * num;
max_prod = num.max(cand1).max(cand2);
min_prod = num.min(cand1).min(cand2);
result = result.max(max_prod);
}
result
}
fn main() {
let nums = vec![-2, -3, -7];
println!("{}", max_product(nums));
}Attempts:
2 left
💡 Hint
Multiplying two negatives can produce a positive product.
✗ Incorrect
The maximum product subarray is [-3, -7] with product 21.
The code tracks max and min products to handle negative numbers.
🧠 Conceptual
expertWhy Track Both Maximum and Minimum Products in Maximum Product Subarray?
In the maximum product subarray problem, why do we track both the maximum and minimum products ending at the current position?
Attempts:
2 left
💡 Hint
Think about how negative numbers affect multiplication.
✗ Incorrect
Multiplying a negative minimum product by a negative number can produce a large positive maximum product.
Tracking both helps handle negative numbers correctly.
