Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum profit from a single buy and sell.
DSA C
int maxProfit(int* prices, int pricesSize) {
int minPrice = prices[0];
int maxProfit = 0;
for (int i = 1; i < pricesSize; i++) {
if (prices[i] < [1]) {
minPrice = prices[i];
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxProfit instead of minPrice in the comparison.
Not updating minPrice correctly.
Starting loop from index 0 instead of 1.
✗ Incorrect
We update minPrice when we find a lower price than the current minPrice.
2fill in blank
mediumComplete the code to calculate maximum profit with unlimited transactions.
DSA C
int maxProfit(int* prices, int pricesSize) {
int profit = 0;
for (int i = 1; i < pricesSize; i++) {
if (prices[i] > [1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing prices[i] with prices[i] (always false).
Adding prices[i] instead of difference.
Using wrong index in comparison.
✗ Incorrect
We add profit when today's price is greater than yesterday's price.
3fill in blank
hardFix the error in the code to find max profit with at most two transactions.
DSA C
int maxProfit(int* prices, int pricesSize) {
int firstBuy = INT_MIN, firstSell = 0;
int secondBuy = INT_MIN, secondSell = 0;
for (int i = 0; i < pricesSize; i++) {
firstBuy = (firstBuy > -prices[i]) ? firstBuy : -prices[i];
firstSell = (firstSell > firstBuy + prices[i]) ? firstSell : firstBuy + prices[i];
secondBuy = (secondBuy > firstSell - prices[i]) ? secondBuy : firstSell - prices[i];
secondSell = (secondSell > secondBuy + [1]) ? secondSell : secondBuy + prices[i];
}
return secondSell;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding wrong variable instead of prices[i].
Using wrong comparison operators.
Not initializing variables correctly.
✗ Incorrect
The last line must add prices[i], not any other variable.
4fill in blank
hardFill both blanks to implement max profit with cooldown period.
DSA C
int maxProfit(int* prices, int pricesSize) {
if (pricesSize == 0) return 0;
int sell = 0, prevSell = 0, buy = -prices[0], prevBuy;
for (int i = 1; i < pricesSize; i++) {
prevBuy = buy;
buy = (buy > prevSell - [1]) ? buy : prevSell - prices[i];
prevSell = sell;
sell = (sell > prevBuy + [2]) ? sell : prevBuy + prices[i];
}
return sell;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prices[i] instead of prices[i - 1] in buy update.
Mixing up buy and sell variables.
Not handling cooldown logic properly.
✗ Incorrect
buy uses prevSell - prices[i], sell uses prevBuy + prices[i].
5fill in blank
hardFill all three blanks to implement max profit with transaction fee.
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 + [1]) ? cash : hold + prices[i] - fee;
hold = (hold > cash - [2]) ? hold : cash - prices[i];
}
return cash;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting fee when buying instead of selling.
Mixing up prices[i] and fee in updates.
Not updating cash and hold correctly.
✗ Incorrect
cash updates with hold + prices[i] - fee; hold updates with cash - prices[i].