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 C# code that calculates the maximum profit from a single buy-sell transaction?
DSA C#
int[] prices = {7, 1, 5, 3, 6, 4}; int minPrice = int.MaxValue; int maxProfit = 0; foreach (int price in prices) { if (price < minPrice) minPrice = price; else if (price - minPrice > maxProfit) maxProfit = price - minPrice; } Console.WriteLine(maxProfit);
Attempts:
2 left
💡 Hint
Track the lowest price so far and calculate profit at each step.
✗ Incorrect
The lowest price is 1, and the highest profit is 6 - 1 = 5.
❓ Predict Output
intermediateMaximum Profit with Unlimited Transactions
What is the output of this C# code that calculates the maximum profit from unlimited buy-sell transactions?
DSA C#
int[] prices = {7, 1, 5, 3, 6, 4}; int maxProfit = 0; for (int i = 1; i < prices.Length; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } Console.WriteLine(maxProfit);
Attempts:
2 left
💡 Hint
Add all positive differences between consecutive days.
✗ Incorrect
Profit is (5-1)+(6-3) = 4 + 3 = 7.
❓ Predict Output
advancedMaximum Profit with Cooldown Period
What is the output of this C# code that calculates the maximum profit with a cooldown period after selling?
DSA C#
int[] prices = {1, 2, 3, 0, 2}; int n = prices.Length; int[] sell = new int[n]; int[] buy = new int[n]; buy[0] = -prices[0]; sell[0] = 0; for (int i = 1; i < n; i++) { buy[i] = Math.Max(buy[i - 1], (i - 2 >= 0 ? sell[i - 2] : 0) - prices[i]); sell[i] = Math.Max(sell[i - 1], buy[i - 1] + prices[i]); } Console.WriteLine(sell[n - 1]);
Attempts:
2 left
💡 Hint
Use dynamic programming arrays for buy and sell states with cooldown.
✗ Incorrect
Maximum profit is 3 by buying on day 0 (price 1), selling on day 1 (price 2, profit 1), cooldown on day 2, buying on day 3 (price 0), selling on day 4 (price 2, profit 2).
❓ Predict Output
advancedMaximum Profit with Transaction Fee
What is the output of this C# code that calculates the maximum profit with a transaction fee for each sell?
DSA C#
int[] prices = {1, 3, 2, 8, 4, 9}; int fee = 2; int n = prices.Length; int cash = 0, hold = -prices[0]; for (int i = 1; i < n; i++) { cash = Math.Max(cash, hold + prices[i] - fee); hold = Math.Max(hold, cash - prices[i]); } Console.WriteLine(cash);
Attempts:
2 left
💡 Hint
Track cash and hold states considering fee on sell.
✗ Incorrect
Max profit is 8 after accounting for fees on transactions.
🧠 Conceptual
expertUnderstanding State Transitions in Buy and Sell Stocks with Cooldown
In the buy and sell stocks problem with cooldown, which statement correctly describes the state transition for the 'buy' state on day i?
Attempts:
2 left
💡 Hint
Cooldown means you cannot buy immediately after selling; you must wait one day.
✗ Incorrect
You can buy today only if you rested yesterday (sell[i-2]) or keep holding from before (buy[i-1]).
