Challenge - 5 Problems
Stock Trading Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of single transaction max profit calculation
What is the output of the following Go code that calculates the maximum profit from a single buy-sell transaction?
DSA Go
package main import "fmt" func maxProfit(prices []int) int { minPrice := prices[0] maxProfit := 0 for _, price := range prices { if price < minPrice { minPrice = price } else if profit := price - minPrice; profit > maxProfit { maxProfit = profit } } return maxProfit } func main() { prices := []int{7,1,5,3,6,4} fmt.Println(maxProfit(prices)) }
Attempts:
2 left
💡 Hint
Track the lowest price and calculate profit at each step.
✗ Incorrect
The lowest price is 1, and the highest profit is 6 - 1 = 5.
❓ Predict Output
intermediateOutput of multiple transactions max profit calculation
What is the output of this Go code that calculates the maximum profit with unlimited transactions?
DSA Go
package main import "fmt" func maxProfit(prices []int) int { profit := 0 for i := 1; i < len(prices); i++ { if prices[i] > prices[i-1] { profit += prices[i] - prices[i-1] } } return profit } func main() { prices := []int{7,1,5,3,6,4} fmt.Println(maxProfit(prices)) }
Attempts:
2 left
💡 Hint
Sum all positive differences between consecutive days.
✗ Incorrect
Profits: (5-1)+(6-3) = 4+3=7 total profit.
❓ Predict Output
advancedOutput of max profit with cooldown period
What is the output of this Go code that calculates max profit with a cooldown day after selling?
DSA Go
package main import "fmt" func maxProfit(prices []int) int { n := len(prices) if n <= 1 { return 0 } sell, prevSell, buy := 0, 0, -prices[0] for i := 1; i < n; i++ { prevBuy := buy buy = max(prevSell - prices[i], buy) prevSell = sell sell = max(prevBuy + prices[i], sell) } return sell } func max(a, b int) int { if a > b { return a } return b } func main() { prices := []int{1,2,3,0,2} fmt.Println(maxProfit(prices)) }
Attempts:
2 left
💡 Hint
Use states: buy, sell, cooldown to track profits.
✗ Incorrect
Max profit is 3 by buying day 0, selling day 2, cooldown day 3, buy day 4, sell day 4.
❓ Predict Output
advancedOutput of max profit with transaction fee
What is the output of this Go code that calculates max profit with a transaction fee per sell?
DSA Go
package main import "fmt" func maxProfit(prices []int, fee int) int { n := len(prices) cash, hold := 0, -prices[0] for i := 1; i < n; i++ { cash = max(cash, hold + prices[i] - fee) hold = max(hold, cash - prices[i]) } return cash } func max(a, b int) int { if a > b { return a } return b } func main() { prices := []int{1,3,2,8,4,9} fee := 2 fmt.Println(maxProfit(prices, fee)) }
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
expertMinimum number of transactions to reach target profit
Given a list of daily stock prices and a target profit, which algorithmic approach best finds the minimum number of buy-sell transactions needed to reach at least the target profit?
Attempts:
2 left
💡 Hint
Consider all possible transactions and track minimum count for target profit.
✗ Incorrect
Dynamic programming explores all combinations to find minimum transactions meeting target profit.
