0
0
DSA Cprogramming~10 mins

Buy and Sell Stocks All Variants in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AminPrice
BmaxProfit
CpricesSize
Dprices[0]
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.
2fill in blank
medium

Complete 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'
Aprices[i - 1]
Bprices[i]
CpricesSize
Dprofit
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.
3fill in blank
hard

Fix 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'
AsecondBuy
BfirstSell
Cprices[i]
DfirstBuy
Attempts:
3 left
💡 Hint
Common Mistakes
Adding wrong variable instead of prices[i].
Using wrong comparison operators.
Not initializing variables correctly.
4fill in blank
hard

Fill 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'
Aprices[i]
Bprices[i - 1]
CpricesSize
DprevSell
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.
5fill in blank
hard

Fill 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'
Aprices[i]
Bfee
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.