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++
#include <iostream> #include <vector> #include <algorithm> #include <climits> int maxProfit(std::vector<int>& prices) { int minPrice = INT_MAX; int maxProfit = 0; for (int price : prices) { minPrice = std::min(minPrice, price); maxProfit = std::max(maxProfit, price - minPrice); } return maxProfit; } int main() { std::vector<int> prices = {7, 1, 5, 3, 6, 4}; std::cout << maxProfit(prices) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Track the lowest price so far and calculate profit by subtracting it from current price.
✗ Incorrect
The minimum price is 1 and the maximum 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 with unlimited buy-sell transactions?
DSA C++
#include <iostream> #include <vector> int maxProfit(std::vector<int>& prices) { int profit = 0; for (size_t i = 1; i < prices.size(); ++i) { if (prices[i] > prices[i - 1]) { profit += prices[i] - prices[i - 1]; } } return profit; } int main() { std::vector<int> prices = {7, 1, 5, 3, 6, 4}; std::cout << maxProfit(prices) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Add all positive differences between consecutive days.
✗ Incorrect
Profits are (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 cooldown (one day after selling you cannot buy)?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int maxProfit(std::vector<int>& prices) { if (prices.empty()) return 0; int n = prices.size(); std::vector<int> sell(n, 0), buy(n, 0), cooldown(n, 0); buy[0] = -prices[0]; for (int i = 1; i < n; ++i) { buy[i] = std::max(buy[i-1], cooldown[i-1] - prices[i]); sell[i] = std::max(sell[i-1], buy[i-1] + prices[i]); cooldown[i] = std::max(cooldown[i-1], sell[i-1]); } return std::max(sell[n-1], cooldown[n-1]); } int main() { std::vector<int> prices = {1, 2, 3, 0, 2}; std::cout << maxProfit(prices) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Use three states: buy, sell, cooldown to track profits.
✗ Incorrect
Maximum profit is 3 by buying day 0, selling day 1, cooldown day 2, buying day 3, selling day 4.
❓ 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++
#include <iostream> #include <vector> #include <algorithm> int maxProfit(std::vector<int>& prices, int fee) { int n = prices.size(); int cash = 0, hold = -prices[0]; for (int i = 1; i < n; ++i) { cash = std::max(cash, hold + prices[i] - fee); hold = std::max(hold, cash - prices[i]); } return cash; } int main() { std::vector<int> prices = {1, 3, 2, 8, 4, 9}; int fee = 2; std::cout << maxProfit(prices, fee) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Track cash and hold states, subtract fee on sell.
✗ Incorrect
Profit is 8 by buying at 1, selling at 8 (fee 2), then buying at 4, selling at 9 (fee 2).
🧠 Conceptual
expertUnderstanding State Transitions in Stock Trading with Cooldown
In the cooldown variant of the stock trading problem, which of the following correctly describes the state transition for the 'buy' state on day i?
Attempts:
2 left
💡 Hint
You can only buy after cooldown, not immediately after selling.
✗ Incorrect
The buy state depends on previous buy or cooldown minus current price, ensuring cooldown day after sell.
