Challenge - 5 Problems
Stock Trading Mastery
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 Java code that calculates the maximum profit from a single buy-sell transaction?
DSA Java
public class Solution { public static int maxProfit(int[] prices) { int minPrice = Integer.MAX_VALUE; int maxProfit = 0; for (int price : prices) { if (price < minPrice) { minPrice = price; } else if (price - minPrice > maxProfit) { maxProfit = price - minPrice; } } return maxProfit; } public static void main(String[] args) { int[] prices = {7, 1, 5, 3, 6, 4}; System.out.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 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 Java code that calculates the maximum profit with unlimited buy-sell transactions?
DSA Java
public class Solution { public static int maxProfit(int[] prices) { int profit = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] > prices[i - 1]) { profit += prices[i] - prices[i - 1]; } } return profit; } public static void main(String[] args) { int[] prices = {7, 1, 5, 3, 6, 4}; System.out.println(maxProfit(prices)); } }
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 At Most Two Transactions
What is the output of this Java code that calculates the maximum profit with at most two buy-sell transactions?
DSA Java
public class Solution { public static int maxProfit(int[] prices) { int n = prices.length; if (n == 0) return 0; int[] leftProfits = new int[n]; int minPrice = prices[0]; for (int i = 1; i < n; i++) { minPrice = Math.min(minPrice, prices[i]); leftProfits[i] = Math.max(leftProfits[i - 1], prices[i] - minPrice); } int[] rightProfits = new int[n + 1]; int maxPrice = prices[n - 1]; for (int i = n - 2; i >= 0; i--) { maxPrice = Math.max(maxPrice, prices[i]); rightProfits[i] = Math.max(rightProfits[i + 1], maxPrice - prices[i]); } int maxProfit = 0; for (int i = 0; i < n; i++) { maxProfit = Math.max(maxProfit, leftProfits[i] + rightProfits[i + 1]); } return maxProfit; } public static void main(String[] args) { int[] prices = {3,3,5,0,0,3,1,4}; System.out.println(maxProfit(prices)); } }
Attempts:
2 left
💡 Hint
Split the array into two parts and combine profits.
✗ Incorrect
Best two transactions: buy at 0 sell at 3, buy at 1 sell at 4, total profit 3 + 3 = 6.
❓ Predict Output
advancedMaximum Profit with Cooldown Period
What is the output of this Java code that calculates the maximum profit with cooldown (cannot buy the day after selling)?
DSA Java
public class Solution { public static int maxProfit(int[] prices) { int n = prices.length; if (n <= 1) return 0; int[] sell = new int[n]; int[] buy = new int[n]; buy[0] = -prices[0]; sell[0] = 0; buy[1] = Math.max(-prices[0], -prices[1]); sell[1] = Math.max(0, prices[1] - prices[0]); for (int i = 2; i < n; i++) { buy[i] = Math.max(buy[i - 1], sell[i - 2] - prices[i]); sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); } return sell[n - 1]; } public static void main(String[] args) { int[] prices = {1, 2, 3, 0, 2}; System.out.println(maxProfit(prices)); } }
Attempts:
2 left
💡 Hint
Use arrays to track buy and sell states with cooldown.
✗ Incorrect
Maximum profit is 3 by buying on day 0 (price 1), selling on day 1 (price 2), cooldown on day 2, buying on day 3 (price 0), selling on day 4 (price 2).
🧠 Conceptual
expertUnderstanding State Transitions in Stock Trading with Cooldown
In the cooldown variant of the stock trading problem, which of the following best describes the reason for using sell[i - 2] when calculating buy[i] in the dynamic programming solution?
Attempts:
2 left
💡 Hint
Think about the cooldown day after selling.
✗ Incorrect
The cooldown means you cannot buy immediately after selling, so you must wait one day. Using sell[i - 2] ensures the cooldown day is respected.
