Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum profit from one transaction.
DSA Javascript
function maxProfit(prices) {
let minPrice = Infinity;
let maxProfit = 0;
for (let i = 0; i < prices.length; i++) {
if (prices[i] < [1]) {
minPrice = prices[i];
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using maxProfit instead of minPrice to compare prices.
Not updating minPrice correctly.
Comparing with 0 instead of minPrice.
✗ Incorrect
We update minPrice when we find a price lower than the current minPrice to track the lowest buying price.
2fill in blank
mediumComplete the code to calculate max profit with unlimited transactions.
DSA Javascript
function maxProfit(prices) {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] > [1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing prices[i] with prices[i] instead of prices[i - 1].
Adding profit when prices[i] is not greater than prices[i - 1].
✗ Incorrect
We buy at prices[i - 1] and sell at prices[i] whenever prices[i] is higher to accumulate profit.
3fill in blank
hardFix the error in the code to calculate max profit with at most two transactions.
DSA Javascript
function maxProfit(prices) {
let firstBuy = Infinity, firstProfit = 0;
let secondBuy = Infinity, secondProfit = 0;
for (let price of prices) {
firstBuy = Math.min(firstBuy, price);
firstProfit = Math.max(firstProfit, price - firstBuy);
secondBuy = Math.min(secondBuy, price - [1]);
secondProfit = Math.max(secondProfit, price - secondBuy);
}
return secondProfit;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using firstBuy instead of firstProfit in the subtraction.
Using secondProfit or secondBuy incorrectly.
✗ Incorrect
We subtract firstProfit from price to simulate reinvesting the profit from the first transaction.
4fill in blank
hardFill both blanks to implement max profit with cooldown period.
DSA Javascript
function maxProfit(prices) {
let sell = 0, prevSell = 0, buy = -Infinity;
for (let price of prices) {
let prevBuy = buy;
buy = Math.max(buy, prevSell - [1]);
prevSell = sell;
sell = Math.max(sell, prevBuy + [2]);
}
return sell;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using positive price when buying instead of negative.
Mixing up the order of updates for buy and sell.
✗ Incorrect
We subtract price when buying and add price when selling to track profits correctly with cooldown.
5fill in blank
hardFill all three blanks to implement max profit with transaction fee.
DSA Javascript
function maxProfit(prices, fee) {
let cash = 0, hold = -prices[0];
for (let i = 1; i < prices.length; i++) {
cash = Math.max(cash, hold + [1] - [2]);
hold = Math.max(hold, cash - [3]);
}
return cash;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong price index like i-1 or i+1.
Not subtracting fee when selling.
✗ Incorrect
When selling, subtract fee from prices[i]. When buying, subtract prices[i].
