HARDasked at 12 companies

Best Time to Buy and Sell Stock III

A hard-tier problem at 51% community acceptance, tagged with Array, Dynamic Programming. Reported in interviews at Bolt and 11 others.

Founder's read

Best Time to Buy and Sell Stock III hits your assessment and you realize you can't just greedy-buy-low-sell-high. Apple, Meta, TikTok, and Visa all ask this one. The trick: you get at most two transactions, and you have to track profit across four states as you move through the array. If you blank on the DP recurrence during the live OA, StealthCoder runs invisibly and surfaces a working solution in seconds. The acceptance rate sits at 51%, which means half the candidates who see this problem don't land it cleanly.

Companies asking
12
Difficulty
HARD
Acceptance
51%

Companies that ask "Best Time to Buy and Sell Stock III"

If this hits your live OA

Best Time to Buy and Sell Stock III 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 a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The trap is thinking you can solve this greedily or with a single pass. You can't. You need Dynamic Programming to track four states: max profit after first buy, after first sell, after second buy, after second sell. As you iterate through prices, you update each state based on whether holding or selling gives you more profit. The common miss: forgetting to initialize the buy states to negative (you start with zero money and buy at price zero), or failing to realize you can do zero, one, or two transactions. This is where most candidates get stuck during their OA. StealthCoder is the hedge if the state-machine pattern doesn't click under pressure.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Best Time to Buy and Sell Stock III recycles across companies for a reason. It's hard-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 a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Best Time to Buy and Sell Stock III interview FAQ

Is this actually a hard problem or is it overrated?+

It's rated hard because you can't pattern-match to other stock problems. The two-transaction constraint forces you to think in DP states, not greedy snapshots. The 51% acceptance rate proves it's legitimately tricky. Companies like Apple and Meta ask it specifically because it filters candidates who understand state machines.

What's the difference between Stock II and Stock III?+

Stock II lets you do unlimited transactions. Stock III caps you at two. That constraint forces DP. Stock II can be solved with a greedy one-pass approach. Stock III requires you to track profit across four distinct states as you move through the array. The state-tracking is what makes it hard.

How does the DP state machine actually work?+

Track: max profit after first buy, after first sell, after second buy, after second sell. For each price, update each state: first buy = max(first buy, -price); first sell = max(first sell, first buy + price); second buy = max(second buy, first sell - price); second sell = max(second sell, second buy + price). Initialize buy states to negative infinity.

Will I see this at Snap, Bolt, or Citadel in their OA?+

It appears in the reported ask lists for 12 companies including those three. It's not in every OA, but if you hit it cold and haven't drilled the state machine, you're in trouble. That's why you prep or have a backstop like StealthCoder for the live assessment.

What's the time and space complexity I should aim for?+

O(n) time is standard and expected. Space can be O(1) if you track four variables instead of a DP array, or O(n) if you build a table. Companies like Meta care about the constant-space solution. Either way, the bottleneck isn't complexity, it's understanding the state transitions under time pressure.

Want the actual problem statement? View "Best Time to Buy and Sell Stock III" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.