Challenge - 5 Problems
Stock Trading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code for max profit with one transaction?
Given the prices array, what is the maximum profit from a single buy-sell transaction?
DSA Rust
fn max_profit(prices: Vec<i32>) -> i32 {
let mut min_price = i32::MAX;
let mut max_profit = 0;
for price in prices {
if price < min_price {
min_price = price;
} else if price - min_price > max_profit {
max_profit = price - min_price;
}
}
max_profit
}
fn main() {
let prices = vec![7,1,5,3,6,4];
println!("{}", max_profit(prices));
}Attempts:
2 left
💡 Hint
Track the lowest price and calculate profit at each step.
✗ Incorrect
The best buy is at price 1 and sell at price 6, profit = 5.
❓ Predict Output
intermediateWhat is the output of this Rust code for max profit with unlimited transactions?
Given the prices array, what is the maximum profit from unlimited buy-sell transactions?
DSA Rust
fn max_profit(prices: Vec<i32>) -> i32 {
let mut profit = 0;
for i in 1..prices.len() {
if prices[i] > prices[i - 1] {
profit += prices[i] - prices[i - 1];
}
}
profit
}
fn main() {
let prices = vec![7,1,5,3,6,4];
println!("{}", max_profit(prices));
}Attempts:
2 left
💡 Hint
Sum all positive differences between consecutive days.
✗ Incorrect
Profit is (5-1)+(6-3) = 4+3 = 7.
❓ Predict Output
advancedWhat is the output of this Rust code for max profit with at most two transactions?
Given the prices array, what is the maximum profit with at most two buy-sell transactions?
DSA Rust
fn max_profit_two_transactions(prices: Vec<i32>) -> i32 {
let n = prices.len();
if n == 0 { return 0; }
let mut left_profits = vec![0; n];
let mut right_profits = vec![0; n + 1];
let mut min_price = prices[0];
for i in 1..n {
min_price = min_price.min(prices[i]);
left_profits[i] = left_profits[i - 1].max(prices[i] - min_price);
}
let mut max_price = prices[n - 1];
for i in (0..n).rev() {
max_price = max_price.max(prices[i]);
right_profits[i] = right_profits[i + 1].max(max_price - prices[i]);
}
let mut max_profit = 0;
for i in 0..n {
max_profit = max_profit.max(left_profits[i] + right_profits[i + 1]);
}
max_profit
}
fn main() {
let prices = vec![3,3,5,0,0,3,1,4];
println!("{}", max_profit_two_transactions(prices));
}Attempts:
2 left
💡 Hint
Use two passes: left to right and right to left profits.
✗ Incorrect
Best two transactions: buy at 3 sell at 5 (0 to 2), buy at 6 sell at 7 (5 to 7), total profit 6.
❓ Predict Output
advancedWhat is the output of this Rust code for max profit with cooldown period?
Given the prices array, what is the maximum profit with cooldown (one day after selling cannot buy)?
DSA Rust
fn max_profit_with_cooldown(prices: Vec<i32>) -> i32 {
let n = prices.len();
if n == 0 { return 0; }
let mut hold = vec![0; n];
let mut sold = vec![0; n];
let mut rest = vec![0; n];
hold[0] = -prices[0];
sold[0] = 0;
rest[0] = 0;
for i in 1..n {
hold[i] = hold[i - 1].max(rest[i - 1] - prices[i]);
sold[i] = hold[i - 1] + prices[i];
rest[i] = rest[i - 1].max(sold[i - 1]);
}
sold[n - 1].max(rest[n - 1])
}
fn main() {
let prices = vec![1,2,3,0,2];
println!("{}", max_profit_with_cooldown(prices));
}Attempts:
2 left
💡 Hint
Track hold, sold, and rest states for each day.
✗ Incorrect
Max profit is 3 by buying day 0 selling day 1 (profit 1), cooldown day 2, buying day 3 selling day 4 (profit 2).
❓ Predict Output
expertWhat is the output of this Rust code for max profit with transaction fee?
Given the prices array and fee, what is the maximum profit with unlimited transactions and fee per transaction?
DSA Rust
fn max_profit_with_fee(prices: Vec<i32>, fee: i32) -> i32 {
let n = prices.len();
if n == 0 { return 0; }
let mut cash = 0;
let mut hold = -prices[0];
for i in 1..n {
cash = cash.max(hold + prices[i] - fee);
hold = hold.max(cash - prices[i]);
}
cash
}
fn main() {
let prices = vec![1,3,2,8,4,9];
let fee = 2;
println!("{}", max_profit_with_fee(prices, fee));
}Attempts:
2 left
💡 Hint
Track cash and hold states, subtract fee on sell.
✗ Incorrect
Transactions: buy at 1 sell at 8 (profit 5 after fee), buy at 4 sell at 9 (profit 3 after fee), total 8.
