Challenge - 5 Problems
Stock Trading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Single Transaction Maximum Profit
What is the output of the following C code that calculates the maximum profit from a single buy-sell transaction given stock prices?
DSA C
int maxProfit(int* prices, int pricesSize) { int minPrice = 2147483647; int maxProfit = 0; for (int i = 0; i < pricesSize; i++) { if (prices[i] < minPrice) { minPrice = prices[i]; } else if (prices[i] - minPrice > maxProfit) { maxProfit = prices[i] - minPrice; } } return maxProfit; } int main() { int prices[] = {7,1,5,3,6,4}; int result = maxProfit(prices, 6); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Think about the lowest price before the highest profit.
✗ Incorrect
The lowest price is 1 and the highest price after it is 6, so max profit is 6 - 1 = 5.
❓ Predict Output
intermediate2:00remaining
Output of Multiple Transactions Maximum Profit
What is the output of this C code that calculates the maximum profit from multiple buy-sell transactions (buy and sell multiple times)?
DSA C
int maxProfit(int* prices, int pricesSize) { int profit = 0; for (int i = 1; i < pricesSize; i++) { if (prices[i] > prices[i - 1]) { profit += prices[i] - prices[i - 1]; } } return profit; } int main() { int prices[] = {7,1,5,3,6,4}; int result = maxProfit(prices, 6); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Sum all positive differences between consecutive days.
✗ Incorrect
Profit is (5-1) + (6-3) = 4 + 3 = 7.
❓ Predict Output
advanced2:30remaining
Output of Maximum Profit with Cooldown
What is the output of this C code that calculates the maximum profit with cooldown (cannot buy on the day after selling)?
DSA C
int maxProfit(int* prices, int pricesSize) { if (pricesSize == 0) return 0; int sell = 0, prev_sell = 0, buy = -2147483647, prev_buy; for (int i = 0; i < pricesSize; i++) { prev_buy = buy; buy = (prev_sell - prices[i]) > buy ? (prev_sell - prices[i]) : buy; prev_sell = sell; sell = (prev_buy + prices[i]) > sell ? (prev_buy + prices[i]) : sell; } return sell; } int main() { int prices[] = {1,2,3,0,2}; int result = maxProfit(prices, 5); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Consider cooldown day after selling.
✗ Incorrect
The best sequence is buy on day 0, sell day 1, cooldown day 2, buy day 3, sell day 4 for profit 3.
❓ Predict Output
advanced2:30remaining
Output of Maximum 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 maxProfit(int* prices, int pricesSize, int fee) { int cash = 0, hold = -prices[0]; for (int i = 1; i < pricesSize; i++) { cash = cash > hold + prices[i] - fee ? cash : hold + prices[i] - fee; hold = hold > cash - prices[i] ? hold : cash - prices[i]; } return cash; } int main() { int prices[] = {1,3,2,8,4,9}; int result = maxProfit(prices, 6, 2); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Subtract fee on each sell transaction.
✗ Incorrect
Transactions: buy at 1 sell at 8 profit 5 (8-1-2), buy at 4 sell at 9 profit 3 (9-4-2), total 8.
🧠 Conceptual
expert3:00remaining
Minimum Number of Transactions for Maximum Profit
Given an array of stock prices, what is the minimum number of transactions needed to achieve the maximum possible profit when multiple transactions are allowed?
Attempts:
2 left
💡 Hint
Think about buying at valleys and selling at peaks.
✗ Incorrect
Each transaction corresponds to buying at a local minimum and selling at the next local maximum to maximize profit with minimum transactions.