Challenge - 5 Problems
Stock Trading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateMaximum Profit with One Transaction
What is the output of the following code that calculates the maximum profit from a single buy-sell transaction?
DSA Python
def max_profit(prices): min_price = float('inf') max_profit = 0 for price in prices: if price < min_price: min_price = price elif price - min_price > max_profit: max_profit = price - min_price return max_profit print(max_profit([7,1,5,3,6,4]))
Attempts:
2 left
💡 Hint
Track the lowest price and calculate profit by subtracting it from current price.
✗ Incorrect
The lowest price is 1, the highest price after that is 6, so max profit is 6 - 1 = 5.
❓ Predict Output
intermediateMaximum Profit with Unlimited Transactions
What is the output of this code that calculates the maximum profit with unlimited buy-sell transactions?
DSA Python
def max_profit(prices): profit = 0 for i in range(1, len(prices)): if prices[i] > prices[i-1]: profit += prices[i] - prices[i-1] return profit print(max_profit([7,1,5,3,6,4]))
Attempts:
2 left
💡 Hint
Add all positive differences between consecutive days.
✗ Incorrect
Profits: (5-1)=4 + (6-3)=3 total 7.
❓ Predict Output
advancedMaximum Profit with At Most Two Transactions
What is the output of this code that calculates the maximum profit with at most two buy-sell transactions?
DSA Python
def max_profit(prices): n = len(prices) if n == 0: return 0 left_profits = [0]*n right_profits = [0]*n min_price = prices[0] for i in range(1, n): min_price = min(min_price, prices[i]) left_profits[i] = max(left_profits[i-1], prices[i] - min_price) max_price = prices[-1] for i in range(n-2, -1, -1): max_price = max(max_price, prices[i]) right_profits[i] = max(right_profits[i+1], max_price - prices[i]) max_profit = 0 for i in range(n): max_profit = max(max_profit, left_profits[i] + right_profits[i]) return max_profit print(max_profit([3,3,5,0,0,3,1,4]))
Attempts:
2 left
💡 Hint
Use two arrays to track max profit from left and right sides.
✗ Incorrect
Best two transactions: buy at index 3 (price 0) sell at index 5 (price 3), buy at index 6 (price 1) sell at index 7 (price 4), total profit 3 + 3 = 6.
❓ Predict Output
advancedMaximum Profit with Cooldown
What is the output of this code that calculates the maximum profit with cooldown (one day after selling you cannot buy)?
DSA Python
def max_profit(prices): if not prices: return 0 n = len(prices) sell, prev_sell = 0, 0 buy, prev_buy = -prices[0], 0 for i in range(1, n): prev_buy = buy buy = max(buy, prev_sell - prices[i]) prev_sell = sell sell = max(sell, prev_buy + prices[i]) return sell print(max_profit([1,2,3,0,2]))
Attempts:
2 left
💡 Hint
Track buy and sell states with cooldown constraints.
✗ Incorrect
Transactions: buy at 1, sell at 3, cooldown day 4, buy at 0, sell at 2, total profit 3.
🧠 Conceptual
expertUnderstanding State Transitions in Stock Trading with Cooldown
In the cooldown variant of the stock trading problem, which state transition correctly represents the update of the 'buy' state on day i?
Attempts:
2 left
💡 Hint
Remember cooldown means you cannot buy immediately after selling the previous day.
✗ Incorrect
You can only buy today if you were not holding stock yesterday or you sold stock two days ago (due to cooldown). So buy[i] depends on sell[i-2].
