Best Time to Buy and Sell Stock with Transaction Fee
A medium-tier problem at 71% community acceptance, tagged with Array, Dynamic Programming, Greedy. Reported in interviews at Citadel and 2 others.
You're hitting the stock problem again. This time there's a transaction fee, and the greedy trick from the simpler version doesn't work anymore. Best Time to Buy and Sell Stock with Transaction Fee appears in assessments at Citadel, Apple, and Goldman Sachs. The trick is recognizing this is a dynamic programming problem disguised as a greedy one. Most candidates see the price array and immediately try to buy-sell-buy-sell, then realize the fee breaks their logic. That's the wall you hit in a live OA. StealthCoder surfaces the state machine approach in seconds if you blank.
Companies that ask "Best Time to Buy and Sell Stock with Transaction Fee"
Best Time to Buy and Sell Stock with Transaction Fee 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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe core insight: you have two states at each price point, holding stock or not holding, and the fee applies every time you sell. You can't just grab every upswing; the fee eats into profit, so you need to decide whether each transaction is worth it. The DP recurrence is simple: for each price, either you hold (take the max of holding yesterday or buying today) or you don't hold (take the max of not holding yesterday or selling today, minus the fee). The greedy intuition fails because a small upswing might not cover the fee cost. This is Array and Dynamic Programming working together, though a Greedy framing exists if you think of it as 'only buy-sell if gain exceeds fee'. The acceptance rate is solid at 70 percent, meaning most people who attempt it solve it, but the state machine pattern trips up candidates who haven't practiced DP. If you freeze during your OA, StealthCoder runs invisibly and hands you the working solution.
Pattern tags
You know the problem.
Make sure you actually pass it.
Best Time to Buy and Sell Stock with Transaction Fee 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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Best Time to Buy and Sell Stock with Transaction Fee interview FAQ
Is this really a DP problem or can I greedy it?+
Both work, but DP is safer. Greedy says 'only transact if profit exceeds fee,' but that requires careful state tracking. DP formulation (hold vs. not-hold at each price) is bulletproof and harder to mess up under pressure. Most solutions lean DP for clarity.
How does the fee change the classic stock problem?+
In the fee-less version, you buy and sell at every local peak-valley pair. Here, the fee is a hard cost per sale, so you skip small upswings. The fee flattens the profit landscape and forces you to be selective about which transactions you actually make.
Do Citadel and Goldman Sachs ask this the same way?+
Yes, it's a standard stock-trading variant at both. The problem statement is consistent: prices array, transaction fee as an integer, return max profit. Both test your ability to code the state machine cleanly under time pressure.
What's the most common mistake?+
Forgetting that you can only hold one share at a time, or applying the fee inconsistently (e.g., charging it on buy instead of sell). Another trap is not initializing your base cases correctly, the first price should set your 'hold' state, not assume you started with cash.
Is 70 percent acceptance high for a Medium?+
Yes, it's on the easier end of Medium. Most people who see the DP pattern recognize it and code it. The wall is typically recognizing the pattern in the first place, not the implementation.
Want the actual problem statement? View "Best Time to Buy and Sell Stock with Transaction Fee" on LeetCode →