DSA interview question
Your trading bot's compliance module now has a new rule from the exchange: after selling a stock, the bot must wait one full day (a "cooldown") before it's allowed to buy again — this mimics a real-world T+1 settlement restriction. Given an array prices where prices[i] is the stock price on day i, you can buy and sell as many times as you want (holding at most one share at a time), but after each sell, you cannot buy on the very next day. Return the maximum profit achievable under this constraint.
int maxProfit(vector<int>& prices);
Input prices | Output | Explanation |
|---|---|---|
[1,2,3,0,2] | 3 | Buy@1, sell@2 (profit 1), cooldown day, buy@0, sell@2 (profit 2) → total 3 |
[1] | 0 | Single day — no transaction possible |
[] | 0 | Empty array |
[1,2,4] | 3 | Buy@1, sell@4 — one transaction beats splitting since cooldown wastes a day |
[6,1,3,2,4,7] | 6 | Buy@1, sell@3 (profit 2), cooldown, buy@2, sell@7 (profit 5)... verify best combo with code |
[7,6,4,3,1] | 0 | Strictly decreasing — no profitable trade |
[2,1,4] | 3 | Buy@1, sell@4 |
(Always verify with the code output since cooldown interactions can be non-obvious to compute by hand.)
prices? (Confirms whether O(n) with O(1) space is expected, or O(n) with O(n) space is acceptable.)On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in