Get Best Price
Reported by candidates from Confluent's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Confluent asked this in April 2024 and it's a trap disguised as simple. You have a stock price sequence and need to find the best profit you can make. Sounds like buy-low-sell-high, right. The catch is the constraints and whether you're allowed multiple transactions. Most candidates overthink the state machine or miss an edge case on what "best price" actually means in context. If you blank on the approach during the live OA, StealthCoder will spot the pattern and surface the right greedy or dynamic-programming path instantly.
Pattern and pitfall
This is almost always a greedy or DP problem depending on transaction rules. If you can make unlimited transactions, greedy works: sum all positive differences between consecutive days. If it's one transaction max, you track the minimum price seen so far and the maximum profit ending at each day. The trick candidates miss: reading whether you must hold the stock between buy and sell, or if you can day-trade, or if there's a cooldown. The algorithmic pattern is either greedy (unlimited) or single-pass tracking (one transaction). During the OA, you'll see the exact constraint in the problem statement. StealthCoder will parse that and show you whether to iterate once and accumulate gains or maintain a min-max pointer. That clarity is the difference between a blank stare and moving forward.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Get Best Price 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderThis 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 Confluent's OA.
Confluent reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Best Price FAQ
Is this the classic 'best time to buy and sell stock' problem?+
Likely, yes. But Confluent's version may add constraints like transaction fees, cooldown periods, or a limit on transaction count. Read the full problem carefully. The core pattern is the same: greedy accumulation or single-pass DP. The constraint makes the implementation differ.
What if I can make unlimited transactions?+
Greedy wins. Sum every positive difference between consecutive prices. That's it. Code is three lines. If you see prices = [1, 2, 3, 0, 2], profit is (2-1) + (3-2) + (2-0) = 4. No hidden complexity.
What if it's limited to one transaction?+
Track the minimum price as you iterate. For each price, calculate profit if you sold at that price. Keep the max profit seen. One pass, O(n) time, O(1) space. That's the expected answer.
How do I prepare for this in 48 hours?+
Don't memorize. Understand the greedy pattern (unlimited) and the min-tracking pattern (one transaction). Code both in your language of choice once. Then read problem constraints obsessively. The pattern doesn't change. The constraint does.
What's the most common mistake candidates make?+
Overcomplicating with DP when greedy works, or misreading whether you can hold across days. Also: off-by-one errors on array indexing. Slow down, read the problem twice, then code. Speed kills here.