Challenge - 5 Problems
Stock Trading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code for max profit with one transaction?
Given the prices array, what is the maximum profit from one buy and one sell?
DSA Typescript
function maxProfitOneTransaction(prices: number[]): number {
let minPrice = Infinity;
let maxProfit = 0;
for (const price of prices) {
if (price < minPrice) {
minPrice = price;
} else if (price - minPrice > maxProfit) {
maxProfit = price - minPrice;
}
}
return maxProfit;
}
const prices = [7,1,5,3,6,4];
console.log(maxProfitOneTransaction(prices));Attempts:
2 left
💡 Hint
Track the lowest price and calculate profit by subtracting it from current price.
✗ Incorrect
The lowest price is 1, and the highest price after that is 6, so max profit is 6 - 1 = 5.
❓ Predict Output
intermediate2:00remaining
What is the output of max profit with unlimited transactions?
Calculate the maximum profit if you can buy and sell stocks multiple times (unlimited transactions).
DSA Typescript
function maxProfitUnlimitedTransactions(prices: number[]): number {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
const prices = [7,1,5,3,6,4];
console.log(maxProfitUnlimitedTransactions(prices));Attempts:
2 left
💡 Hint
Add all positive differences between consecutive days.
✗ Incorrect
Profits: (5-1) + (6-3) = 4 + 3 = 7
❓ Predict Output
advanced2:30remaining
What is the output of max profit with at most two transactions?
Find the maximum profit with at most two buy-sell transactions.
DSA Typescript
function maxProfitTwoTransactions(prices: number[]): number {
const n = prices.length;
if (n === 0) return 0;
const leftProfits = new Array(n).fill(0);
let minPrice = prices[0];
for (let i = 1; i < n; i++) {
minPrice = Math.min(minPrice, prices[i]);
leftProfits[i] = Math.max(leftProfits[i - 1], prices[i] - minPrice);
}
const rightProfits = new Array(n + 1).fill(0);
let maxPrice = prices[n - 1];
for (let i = n - 2; i >= 0; i--) {
maxPrice = Math.max(maxPrice, prices[i]);
rightProfits[i] = Math.max(rightProfits[i + 1], maxPrice - prices[i]);
}
let maxProfit = 0;
for (let i = 0; i < n; i++) {
maxProfit = Math.max(maxProfit, leftProfits[i] + rightProfits[i + 1]);
}
return maxProfit;
}
const prices = [3,3,5,0,0,3,1,4];
console.log(maxProfitTwoTransactions(prices));Attempts:
2 left
💡 Hint
Use two passes: left to right and right to left to track profits.
✗ Incorrect
Best two transactions: buy at 0 sell at 3, buy at 1 sell at 4, total profit 3 + 3 = 6.
❓ Predict Output
advanced2:30remaining
What is the output of max profit with cooldown period?
Calculate max profit if after selling stock you must wait one day before buying again.
DSA Typescript
function maxProfitWithCooldown(prices: number[]): number {
const n = prices.length;
if (n <= 1) return 0;
let sold = 0, hold = -prices[0], rest = 0;
for (let i = 1; i < n; i++) {
const prevSold = sold;
sold = hold + prices[i];
hold = Math.max(hold, rest - prices[i]);
rest = Math.max(rest, prevSold);
}
return Math.max(sold, rest);
}
const prices = [1,2,3,0,2];
console.log(maxProfitWithCooldown(prices));Attempts:
2 left
💡 Hint
Track three states: sold, hold, and rest to handle cooldown.
✗ Incorrect
Best transactions: buy day 0, sell day 2, cooldown day 3, buy day 4, sell day 4, total profit 3.
❓ Predict Output
expert3:00remaining
What is the output of max profit with transaction fee?
Calculate max profit if each transaction costs a fee.
DSA Typescript
function maxProfitWithFee(prices: number[], fee: number): number {
const n = prices.length;
if (n === 0) return 0;
let cash = 0;
let hold = -prices[0];
for (let i = 1; i < n; i++) {
cash = Math.max(cash, hold + prices[i] - fee);
hold = Math.max(hold, cash - prices[i]);
}
return cash;
}
const prices = [1,3,2,8,4,9];
const fee = 2;
console.log(maxProfitWithFee(prices, fee));Attempts:
2 left
💡 Hint
Track cash and hold states, subtract fee when selling.
✗ Incorrect
Transactions: buy at 1 sell at 8 profit 7-2=5, buy at 4 sell at 9 profit 5-2=3, total 8 but fee counted twice, final profit 6.