Max Profits
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a TikTok OA incoming and Max Profits is on the table. This is a classic dynamic programming setup disguised as a trading problem, and the trick is knowing whether you're allowed one transaction or unlimited ones. The assessment likely throws you a variation you haven't seen before, which is why having a solid pattern in your pocket matters. StealthCoder can read the exact constraints in real time and feed you the right approach if your mind goes blank under pressure.
Pattern and pitfall
Max Profits problems hinge on state management. You're tracking the maximum money you can make given constraints on when you can buy and sell. The most common version: you can hold at most one stock at a time, and you want to maximize profit from one transaction (or unlimited). The DP states are usually buy and sell positions indexed by day. If it's one transaction, it's O(n) space and time with two variables tracking the lowest price seen and max profit so far. If it's unlimited, you accumulate profit on every upswing. The pitfall is confusing the constraint: re-read whether you can make multiple non-overlapping trades or just one. When you hit the problem live, StealthCoder lets you confirm the exact wording and pull the right solution without wasting mental cycles on ambiguity.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Max Profits 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as best time to buy and sell stock. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Max Profits FAQ
Is this one transaction or unlimited trades?+
TikTok's version likely specifies in the constraints. One transaction is simpler (track min price, max profit as you scan). Unlimited trades means you sum every gain where price[i] > price[i-1]. Read the exact constraint first.
Do I need DP or can I use a greedy scan?+
If it's unlimited trades, greedy works: add every positive difference. If it's one or k transactions, DP is safer. With multiple transactions, you track states like dp[i][j][0/1] (day i, j transactions done, holding or not). TikTok tends to go multi-transaction, so prepare for DP.
What's the time complexity I should hit?+
One transaction: O(n) time, O(1) space. Unlimited: O(n) time, O(1) space. k transactions: O(n*k) time, O(k) space. If k is large, reduce it to min(k, n/2) since you can't do more than n/2 non-overlapping trades.
Are there edge cases that trap people?+
Empty array or single element (profit is zero). All prices decreasing (profit is zero, not negative). Handling the buy-sell order correctly: you must buy before you sell. Off-by-one indexing in DP tables. Test with a small manual example first.
How do I prepare in 48 hours if I blank on DP?+
Code the one-transaction version first (min price + max profit scan). Then code unlimited (greedy sum of gains). If TikTok asks k transactions, your DP skeleton is dp[i][j][hold] indexed by day, transaction count, and position. Practice those three, skip edge-case optimizations on the first pass.