0
0
DSA Cprogramming~20 mins

Buy and Sell Stocks All Variants in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stock Trading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Single Transaction Maximum Profit
What is the output of the following C code that calculates the maximum profit from a single buy-sell transaction given stock prices?
DSA C
int maxProfit(int* prices, int pricesSize) {
    int minPrice = 2147483647;
    int maxProfit = 0;
    for (int i = 0; i < pricesSize; i++) {
        if (prices[i] < minPrice) {
            minPrice = prices[i];
        } else if (prices[i] - minPrice > maxProfit) {
            maxProfit = prices[i] - minPrice;
        }
    }
    return maxProfit;
}

int main() {
    int prices[] = {7,1,5,3,6,4};
    int result = maxProfit(prices, 6);
    printf("%d\n", result);
    return 0;
}
A5
B4
C6
D7
Attempts:
2 left
💡 Hint
Think about the lowest price before the highest profit.
Predict Output
intermediate
2:00remaining
Output of Multiple Transactions Maximum Profit
What is the output of this C code that calculates the maximum profit from multiple buy-sell transactions (buy and sell multiple times)?
DSA C
int maxProfit(int* prices, int pricesSize) {
    int profit = 0;
    for (int i = 1; i < pricesSize; i++) {
        if (prices[i] > prices[i - 1]) {
            profit += prices[i] - prices[i - 1];
        }
    }
    return profit;
}

int main() {
    int prices[] = {7,1,5,3,6,4};
    int result = maxProfit(prices, 6);
    printf("%d\n", result);
    return 0;
}
A4
B5
C6
D7
Attempts:
2 left
💡 Hint
Sum all positive differences between consecutive days.
Predict Output
advanced
2:30remaining
Output of Maximum Profit with Cooldown
What is the output of this C code that calculates the maximum profit with cooldown (cannot buy on the day after selling)?
DSA C
int maxProfit(int* prices, int pricesSize) {
    if (pricesSize == 0) return 0;
    int sell = 0, prev_sell = 0, buy = -2147483647, prev_buy;
    for (int i = 0; i < pricesSize; i++) {
        prev_buy = buy;
        buy = (prev_sell - prices[i]) > buy ? (prev_sell - prices[i]) : buy;
        prev_sell = sell;
        sell = (prev_buy + prices[i]) > sell ? (prev_buy + prices[i]) : sell;
    }
    return sell;
}

int main() {
    int prices[] = {1,2,3,0,2};
    int result = maxProfit(prices, 5);
    printf("%d\n", result);
    return 0;
}
A5
B4
C3
D2
Attempts:
2 left
💡 Hint
Consider cooldown day after selling.
Predict Output
advanced
2:30remaining
Output of Maximum Profit with Transaction Fee
What is the output of this C code that calculates the maximum profit with a transaction fee for each sell?
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 + prices[i] - fee ? cash : hold + prices[i] - fee;
        hold = hold > cash - prices[i] ? hold : cash - prices[i];
    }
    return cash;
}

int main() {
    int prices[] = {1,3,2,8,4,9};
    int result = maxProfit(prices, 6, 2);
    printf("%d\n", result);
    return 0;
}
A6
B8
C5
D7
Attempts:
2 left
💡 Hint
Subtract fee on each sell transaction.
🧠 Conceptual
expert
3:00remaining
Minimum Number of Transactions for Maximum Profit
Given an array of stock prices, what is the minimum number of transactions needed to achieve the maximum possible profit when multiple transactions are allowed?
ANumber of local minima and maxima pairs
BNumber of all increasing price pairs
CNumber of days minus one
DNumber of price peaks only
Attempts:
2 left
💡 Hint
Think about buying at valleys and selling at peaks.