Best Time to Buy and Sell Stock II
A medium-tier problem at 70% community acceptance, tagged with Array, Dynamic Programming, Greedy. Reported in interviews at CTC and 33 others.
Best Time to Buy and Sell Stock II shows up at Apple, Amazon, Intel, and 31 other companies. It's a medium-difficulty problem with a 69.5% acceptance rate, which means it's common enough that you'll see it in real assessments but still trips up candidates who overthink it. The trap is thinking you need dynamic programming or a complex state machine. You don't. The actual solution is so much simpler that candidates who haven't drilled the greedy pattern often blank on it live. If this problem hits your online assessment and you freeze, StealthCoder surfaces the working solution in seconds, invisible to the proctor.
Companies that ask "Best Time to Buy and Sell Stock II"
Best Time to Buy and Sell Stock II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe problem asks you to maximize profit by buying and selling a stock multiple times. Most candidates start building a DP table or trying to track complex states. The trick is realizing you can capture every upward price movement by buying before each increase and selling at the peak. This greedy approach works because you're allowed unlimited transactions and there's no transaction fee. The pattern lives in Array and Greedy logic, though some solutions frame it as Dynamic Programming. Common pitfall: candidates overthink transaction ordering or try to optimize which peaks to capture, missing that you capture all of them. The greedy solution is O(n) time and O(1) space, beating any DP approach. When you hit this live and the obvious state-machine doesn't click, StealthCoder gives you the greedy insight and a clean implementation to paste.
Pattern tags
You know the problem.
Make sure you actually pass it.
Best Time to Buy and Sell Stock II recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Best Time to Buy and Sell Stock II interview FAQ
Is this really just greedy, or do I need DP?+
It's greedy. DP works but it's overkill. The insight is that every single upward gap between consecutive days is a profit you can capture. Buy before the rise, sell at the top. Repeat for all rises. O(n) greedy beats O(n) DP on clarity and space.
What's the main thing candidates get wrong?+
Overthinking transaction order and trying to predict which peaks matter most. The answer is simpler: all peaks matter equally. Capture every rise, no matter how small. Once you see that pattern, the code is five lines.
Do big tech companies still ask this?+
Yes. Apple, Amazon, Intel, and Deutsche Bank all report asking it. It's a screening problem for medium-level roles. High acceptance rate means it's fair but the greedy trick is the gatekeeper.
How does this fit with the other stock problems?+
Stock I (one transaction, DP/greedy hybrid) is harder. Stock II (unlimited transactions, greedy) is cleaner. Stock III (two transactions, DP required) is harder still. Stock II is the sweet spot where greedy elegance shines.
Can I solve this without seeing the greedy pattern first?+
Technically yes, using DP with states for hold/sold, but it's slower to code and reason through. The greedy pattern is the intended solution. If you blank on it live, StealthCoder delivers the working approach in seconds, invisible to the proctor.
Want the actual problem statement? View "Best Time to Buy and Sell Stock II" on LeetCode →