0
0
DSA Typescriptprogramming~10 mins

Buy and Sell Stocks All Variants in DSA Typescript - 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-sell transaction.

DSA Typescript
function maxProfit(prices: number[]): number {
  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;
}
Drag options to blanks, or click blank then click option'
AminPrice
Bprices[i]
CmaxProfit
DInfinity
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxProfit instead of minPrice in the comparison.
Comparing prices[i] with Infinity directly.
2fill in blank
medium

Complete the code to calculate maximum profit with unlimited transactions (buy and sell multiple times).

DSA Typescript
function maxProfit(prices: number[]): number {
  let profit = 0;
  for (let i = 1; i < prices.length; 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]
C0
Dprices[i + 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing prices[i] with prices[i] (always false).
Using prices[i + 1] which may be out of bounds.
3fill in blank
hard

Fix the error in the code to find max profit with at most two transactions.

DSA Typescript
function maxProfit(prices: number[]): number {
  let firstBuy = Infinity, firstProfit = 0;
  let secondBuy = Infinity, secondProfit = 0;
  for (const 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;
}
Drag options to blanks, or click blank then click option'
AsecondProfit
BsecondBuy
CfirstBuy
DfirstProfit
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting firstBuy instead of firstProfit.
Using secondProfit or secondBuy incorrectly.
4fill in blank
hard

Fill both blanks to implement max profit with cooldown (cannot buy immediately after selling).

DSA Typescript
function maxProfit(prices: number[]): number {
  let sell = 0, prevSell = 0, buy = -Infinity, prevBuy;
  for (const price of prices) {
    prevBuy = buy;
    buy = Math.max(prevBuy, [1] - price);
    prevSell = sell;
    sell = Math.max(prevSell, [2] + price);
  }
  return sell;
}
Drag options to blanks, or click blank then click option'
AprevSell
Bsell
CprevBuy
Dbuy
Attempts:
3 left
💡 Hint
Common Mistakes
Using sell instead of prevSell for buying.
Using buy instead of prevBuy for selling.
5fill in blank
hard

Fill both blanks to implement max profit with transaction fee.

DSA Typescript
function maxProfit(prices: number[], fee: number): number {
  let cash = 0, hold = -prices[0];
  for (let i = 1; i < prices.length; i++) {
    cash = Math.max(cash, hold + prices[i] - [1]);
    hold = Math.max(hold, cash - [2]);
  }
  return cash;
}
Drag options to blanks, or click blank then click option'
Afee
Bprices[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Not subtracting fee when selling.
Subtracting fee when buying instead of price.