Best Stock Profit With a Sell Fee
Reported by candidates from Kaseya's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The fixed sellFee on every sale is the whole twist in Kaseya's September 2026 OA report. It's the classic buy-and-sell-many-times setup, holding at most one share, but each sale costs you. Prices can run to 200000 entries, so anything quadratic dies. If you've seen the stock problems before, this one is a small tweak on a known shape. It's a two-state dynamic programming problem that runs in a single pass. If you blank under the clock, StealthCoder runs invisibly during the live OA as a safety net and hands you the recurrence. Know the two states and you won't need it.
The problem
You may buy and sell one share any number of times, but may hold at most one share. Every sale pays a fixed sellFee. Return the maximum achievable profit. A sale may occur only after its corresponding purchase. Function maxProfitWithSellFee(prices: int[], sellFee: int) → int Examples Example 1 prices = [1,3,2,8,4,9] sellFee = 2 return = 8 Case 1 exercises the documented deterministic contract. Example 2 prices = [1,3,7,5,10,3] sellFee = 3 return = 6 Case 2 exercises the documented deterministic contract. Example 3 prices = [5] sellFee = 2 return = 0 Case 3 exercises the documented deterministic contract. Constraints 1 <= prices.length <= 200000. 0 <= prices[i], sellFee <= 10^6. The answer fits a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Track two values as you scan: cash (best profit holding no share) and hold (best profit holding one share). Start with cash = 0 and hold = -prices[0]. For each price p, update cash = max(cash, hold + p - sellFee), then hold = max(hold, cash - p). Return cash at the end. That's O(n) time and O(1) space. The common pitfall is charging the fee at the wrong moment or both times. Charge it once, on the sale, exactly as the statement says. Another trap is updating hold with a stale cash value. Order matters, or you can use a temp copy. Check Example 1: prices [1,3,2,8,4,9], fee 2 gives 8. Buy at 1, sell at 8 for 5, buy at 4, sell at 9 for 3. Total is 8. Greedy with a running minimum also works but is easier to get wrong. If the recurrence slips away mid-assessment, StealthCoder is the hedge that keeps you moving.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Best Stock Profit With a Sell Fee cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderThis OA pattern shows up on LeetCode as best time to buy and sell stock with transaction fee. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Kaseya's OA.
Kaseya reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Best Stock Profit With a Sell Fee FAQ
How hard is the Kaseya sell fee stock problem really?+
Medium. The shape is the familiar stock DP with a fee. If you know the two-state hold and cash idea, it's about ten lines. The hard part is spotting that greedy peak-and-valley fails once the fee exists, so you need states.
What's the trick to maxProfitWithSellFee?+
Keep two running values: profit with no share and profit holding a share. On each price, either sell now and pay the fee, or keep holding. Then either buy now or keep waiting. One pass, constant space, return the cash state.
Do I need a full DP array for 200000 prices?+
No. Each day only depends on the previous day's two values, so two variables are enough. A full array works but wastes memory. With n up to 200000, O(n) time is required and O(n) space is merely unnecessary.
Where do people usually get wrong answers here?+
Charging the fee on both buy and sell, or applying it only when a trade looks profitable. Also forgetting that holding at most one share means you can't buy again before selling. Test with prices [5] and fee 2, which should return 0.
How do I prepare for this in 48 hours?+
Write the two-state solution from memory twice, then trace Examples 1 and 2 by hand to confirm 8 and 6. Then try variants like a cooldown or a buy fee. Those share the same state-machine idea, so you'll be covered.