The "Best Time to Buy and Sell Stock with Cooldown interview question" introduces a realistic constraint to the stock trading problem. You can complete as many transactions as you like, but after you sell a stock, you cannot buy again on the next day. This one-day "cooldown" period forces you to carefully time your exits to maximize future entry opportunities.
Companies like Goldman Sachs, Microsoft, and Meta ask the "Best Time to Buy and Sell Stock with Cooldown coding problem" to test a candidate's ability to model complex state transitions. It’s a classic "Dynamic Programming" problem where the decision today (to sell or not) has a direct impact on the available actions two days from now. It evaluates "State Machine" design skills.
This problem is solved using Dynamic Programming with State Transition. We define three states for each day :
hold[i] = max(hold[i-1], reset[i-1] - prices[i])sold[i] = hold[i-1] + prices[i]reset[i] = max(reset[i-1], sold[i-1])
The maximum profit on the last day will be the maximum of the sold and reset states.Prices: [1, 2, 3, 0, 2]
hold = -1.sold = 1. (Now in cooldown for Day 3).reset because of cooldown. reset = 1.hold = 1 - 0 = 1.sold = 1 + 2 = 3.
Max Profit: 3.When a problem involves "waiting" or "cooldowns," think of it as a state machine. Draw the states (Hold, Sold, Reset) and the arrows between them. Once the diagram is clear, the "Dynamic Programming" equations almost write themselves.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Partition Array for Maximum Sum | Medium | Solve | |
| Find the Maximum Length of Valid Subsequence I | Medium | Solve | |
| Maximum Absolute Sum of Any Subarray | Medium | Solve | |
| Solving Questions With Brainpower | Medium | Solve | |
| Coin Change II | Medium | Solve |