What is the main goal in the 'Buy and Sell Stocks' problem?
To find the maximum profit by choosing the best days to buy and sell stocks given their prices over time.
Click to reveal answer
beginner
In the 'Buy and Sell Stocks I' variant, how many transactions are allowed?
Only one transaction is allowed: buy once and sell once.
Click to reveal answer
intermediate
What does the 'Buy and Sell Stocks II' variant allow that the first variant does not?
It allows multiple transactions (buy and sell multiple times) to maximize profit.
Click to reveal answer
advanced
How does the 'Buy and Sell Stocks with Cooldown' variant change the problem?
After selling a stock, you must wait one day before buying again (cooldown period).
Click to reveal answer
advanced
What is the key difference in the 'Buy and Sell Stocks with Transaction Fee' variant?
Each transaction (buy + sell) incurs a fee that reduces the profit from that transaction.
Click to reveal answer
In the 'Buy and Sell Stocks I' problem, what is the best strategy to maximize profit?
ABuy at the lowest price and sell at the highest price after it
BBuy and sell every day
CBuy at the highest price and sell at the lowest price
DBuy and hold without selling
✗ Incorrect
You want to buy at the lowest price and sell later at a higher price to maximize profit.
Which variant allows unlimited transactions to maximize profit?
ABuy and Sell Stocks with Cooldown
BBuy and Sell Stocks I
CBuy and Sell Stocks II
DBuy and Sell Stocks with Transaction Fee
✗ Incorrect
Buy and Sell Stocks II allows multiple transactions without restrictions.
What does the cooldown period mean in the stock trading problem?
AYou cannot buy and sell on the same day
BYou must wait one day after selling before buying again
CYou cannot sell stocks
DYou must buy stocks every day
✗ Incorrect
Cooldown means waiting one day after selling before you can buy again.
How does a transaction fee affect the profit calculation?
AIt reduces the profit by the fee amount per transaction
BIt has no effect
CIt doubles the profit
DIt increases the profit
✗ Incorrect
Each transaction reduces profit by the fee amount.
Which approach is commonly used to solve 'Buy and Sell Stocks with Cooldown'?
AGreedy algorithm
BBinary search
CSorting
DDynamic programming
✗ Incorrect
Dynamic programming helps track states with cooldown constraints.
Explain how to find the maximum profit when only one transaction is allowed.
Think about the lowest price before the current day.
You got /3 concepts.
Describe how cooldown affects buying and selling decisions in stock trading.
Consider what happens right after you sell a stock.
You got /3 concepts.
Practice
(1/5)
1. What is the main goal when solving the Buy and Sell Stocks problem with only one transaction allowed?
easy
A. Maximize the difference between a later selling price and an earlier buying price
B. Minimize the number of transactions
C. Find the lowest price in the array
D. Calculate the average stock price
Solution
Step 1: Understand the problem goal
The problem asks to find the maximum profit by buying and selling once, so we want the biggest positive difference between a sell price after a buy price.
Step 2: Identify the correct approach
Maximizing the difference between a later selling price and an earlier buying price directly solves the problem.
Final Answer:
Maximize the difference between a later selling price and an earlier buying price -> Option A
Quick Check:
Goal = Maximize profit from one buy and one sell [OK]
Hint: Look for max difference with sell after buy [OK]
Common Mistakes:
Thinking to minimize transactions instead of maximizing profit
Confusing lowest price with max profit
Ignoring the order of buy before sell
2. Which of the following TypeScript function signatures correctly represents a function to find max profit from multiple stock transactions?
easy
A. function maxProfit(prices: number[]): number
B. function maxProfit(prices: string): number[]
C. function maxProfit(prices: number): string
D. function maxProfit(prices: any): void
Solution
Step 1: Analyze the input and output types
The input should be an array of numbers representing prices, so prices: number[]. The output is the maximum profit, a number.
Step 2: Match the correct function signature
function maxProfit(prices: number[]): number matches input as number array and output as number, which is correct.
Final Answer:
function maxProfit(prices: number[]): number -> Option A
Quick Check:
Input array, output number [OK]
Hint: Input array of numbers, output single number [OK]
Common Mistakes:
Using string type for prices
Returning array instead of number
Using void return type
3. What is the output of the following TypeScript code for max profit with unlimited transactions?
function maxProfit(prices: number[]): number {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
console.log(maxProfit([7,1,5,3,6,4]));
medium
A. 5
B. 7
C. 8
D. 6
Solution
Step 1: Trace the loop and calculate profit increments
4. Identify the bug in this TypeScript code for max profit with one transaction:
function maxProfit(prices: number[]): number {
let minPrice = prices[0];
let maxProfit = 0;
for (let i = 1; i <= prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
medium
A. prices array should be sorted first
B. minPrice should be initialized to 0
C. maxProfit should start at -Infinity
D. Loop condition should be < instead of <=
Solution
Step 1: Check loop boundary
The loop runs with i from 1 to prices.length inclusive, but array indices go from 0 to prices.length - 1. Using <= causes out-of-bounds access at prices[prices.length].
Step 2: Correct the loop condition
Change loop condition to i < prices.length to avoid accessing undefined elements.
Final Answer:
Loop condition should be < instead of <= -> Option D
Quick Check:
Array index out-of-bounds fixed by < condition [OK]
Hint: Use < for array loops, not <= [OK]
Common Mistakes:
Using <= causing runtime errors
Initializing minPrice incorrectly
Sorting prices unnecessarily
5. Given the prices array [3, 3, 5, 0, 0, 3, 1, 4], what is the maximum profit with at most two transactions?
hard
A. 5
B. 6
C. 7
D. 4
Solution
Step 1: Understand two transactions limit
We can buy and sell at most twice. We want to split the array into two parts and maximize profit from each.
Step 2: Calculate max profit for each split
Best first transaction: buy at 3, sell at 5 = 2 profit
Best second transaction: buy at 0, sell at 4 = 4 profit
Total max profit = 2 + 4 = 6
Step 3: Check for better splits
Splitting at index 5: first part max profit = 4 (buy at 0 sell at 3), second part max profit = 3 (buy at 1 sell at 4), total = 7
Final Answer:
7 -> Option C
Quick Check:
Sum of two best transactions = 7 [OK]
Hint: Split array, sum max profits from two parts [OK]
Common Mistakes:
Counting more than two transactions
Not splitting array correctly
Choosing wrong buy/sell days
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.