Reported July 2024
Hudson River Tradinggreedy

Buy and Sell Stock

Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Hudson River Trading OA. Under 2s to a working solution.
Founder's read

Hudson River Trading sent you a Buy and Sell Stock problem in July 2024, and you're staring at it 24 hours before the OA. This is a classic dynamic programming or greedy play, depending on the variant. The trick isn't complex, but there's a behavioral trap: most candidates overthink the state transitions or miss the single-pass opportunity. StealthCoder sits invisibly on your screen during the real OA and feeds you the pattern the moment you blank on the recurrence relation or loop structure.

Pattern and pitfall

Buy and Sell Stock has three main flavors: unlimited transactions (greedy, track the upslope), one transaction (min price seen so far, max profit to current point), and k transactions (DP with states for held/sold status). Hudson River Trading typically goes for the unlimited or one-transaction variant to test whether you recognize that you don't need DP when you can solve greedily or with a single pass. The common pitfall is building a 2D DP table when a scalar or two scalars suffice. The pattern is usually greedy or dynamic programming. If you blank on which during the OA, StealthCoder retrieves the exact template and state machine needed. This isn't a trick question; it's a fundamentals check on optimization awareness.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Buy and Sell Stock 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as best time to buy and sell stock. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Hudson River Trading's OA.

Hudson River Trading 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.

Buy and Sell Stock FAQ

Is this the 'buy once, sell once' version or unlimited transactions?+

The problem title alone doesn't specify. If the statement says you can hold at most one stock at a time, it's likely unlimited transactions (greedy). If it says 'at most one transaction total', it's the simpler O(n) single-pass variant. Read carefully; the constraint defines the approach.

Do I really need DP for this?+

Rarely. One-transaction: track the lowest price and max profit so far in one loop. Unlimited: sum all positive differences between consecutive days (greedy). Only the 'k transactions' variant needs real DP. If you're thinking 2D state, you've overthought it.

What's the trap Hudson River will test?+

Handling edge cases: empty array, single element, all prices falling (profit is zero, not negative). Also, candidates often hardcode a state machine when the greedy insight is simpler. Clarity and correctness over micro-optimizations.

How do I nail this in under 15 minutes?+

Clarify the constraint (one transaction or unlimited). Write a single-pass loop. Track two numbers: min price seen and max profit so far (or hold/sell states if unlimited). Test with edge cases. Done. No DP tables unless the problem explicitly says 'k transactions'.

Will they ask follow-ups after I solve it?+

Likely. They might ask for the k-transaction variant, or about cooldown, or fee per transaction. If you nail the base case, you're in a strong position. Practice the greedy and one-pass DP templates; they're portable.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Hudson River Trading.

OA at Hudson River Trading?
Invisible during screen share
Get it