Best Time to Buy and Sell Stock IV
A hard-tier problem at 47% community acceptance, tagged with Array, Dynamic Programming. Reported in interviews at Nielsen and 8 others.
Best Time to Buy and Sell Stock IV is the hardest problem in the stock trading series, and companies like Apple, Google, Goldman Sachs, and Nvidia have all asked it. The catch: you're given a limit on how many transactions you can make, and a transaction is a buy-sell pair. The obvious greedy approach fails. You need dynamic programming to track state across multiple transactions, and the state space explodes if you don't optimize. With a 47% acceptance rate, most candidates hit a wall on the constraint handling. If this problem lands in your live OA and you haven't drilled the DP formulation, StealthCoder solves it invisibly in seconds.
Companies that ask "Best Time to Buy and Sell Stock IV"
Best Time to Buy and Sell Stock IV 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 Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe core trick is recognizing that without the transaction limit, you'd buy at every local minimum and sell at every local maximum. But with a cap, you can't. You need DP where the state is: position in the array, number of transactions left, and whether you currently hold stock. The recurrence is then: at each step, you can hold, buy (if you have transactions), sell (if you hold), or pass. Naive memoization creates a 3D DP table that can be huge. The optimization is to either compress it based on remaining transactions, or swap to BFS with state pruning. Common pitfalls: treating a buy-sell as two separate steps rather than one transaction, or not recognizing when k is large enough to become the unlimited case. StealthCoder surfaces the right state variables and base cases so you don't waste time debugging the DP during the real assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Best Time to Buy and Sell Stock IV 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 an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Best Time to Buy and Sell Stock IV interview FAQ
How does the transaction limit actually change the problem?+
Without a limit, greedy works: buy before every rise, sell before every fall. With a limit k, you must choose which k rises to capture. This forces you to track state (position, transactions used, holding status) and decide whether to use a transaction now or save it. It's why DP replaces greedy.
Is this still asked at FAANG?+
Yes. Apple, Google, and Goldman Sachs all appear in the request data. It's a proven FAANG hard problem. The 47% acceptance rate reflects its genuine difficulty, not a trick question. Expect it.
What's the key optimization to avoid TLE?+
If k is greater than or equal to half the array length, you can do unlimited transactions and skip the DP entirely. Also, compress your DP table: instead of 3D, use rolling arrays or online updates. This cuts space from O(n*k*2) to O(k*2) or O(k).
How does this relate to the Dynamic Programming topic?+
This is a textbook DP problem: optimal substructure (best k trades ending at day i depends on best k-1 trades before day i) and overlapping subproblems. You define the recurrence, then memoize or tabulate. It's where DP stops being theoretical and becomes essential.
What's the most common bug when coding the solution?+
Counting a transaction wrong. A transaction is buy + sell, not two steps. Also, forgetting to handle the case where k >= n/2 (unlimited trades). And off-by-one errors in the recurrence when you initialize or transition between states.
Want the actual problem statement? View "Best Time to Buy and Sell Stock IV" on LeetCode →