Reported December 2024
IBMgreedy

Max Profit

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

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

You've got an IBM OA coming up in the next day or two, and Max Profit just showed up in December reports. This is a greedy problem dressed up in market language. The core idea: you're buying and selling something (stock, currency, asset, doesn't matter) and you need to find the maximum profit you can extract. The trick isn't complex math or dynamic programming backtracking. It's spotting the pattern in the prices and moving fast. If you blank during the live OA, StealthCoder reads the exact problem and gives you the pattern so you don't waste 20 minutes guessing.

Pattern and pitfall

Max Profit is almost always one of two flavors: single transaction (buy once, sell once, find max difference) or multiple transactions (buy and sell as many times as you want, pocket every upswing). The greedy insight: if you can transact multiple times, you just buy before every price jump and sell at the peak. Sum all the positive differences. If it's single transaction, it's a one-pass scan tracking the min price seen so far and the max profit ending at each position. The gotcha candidates hit: overthinking it as DP when greedy works. Or missing edge cases like empty arrays or all-declining prices. When you're live and the problem wording is ambiguous, StealthCoder sees the exact constraint and nails which version you're solving.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Max Profit 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 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 IBM's OA.

IBM 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.

Max Profit FAQ

Is this the stock buy/sell problem from LeetCode or something different?+

IBM tends to use stock market framing, but the underlying pattern is identical to LeetCode 121 or 122 (single vs. multiple transactions). The greedy approach is the same. If the problem says 'you can transact multiple times,' sum the positive deltas. Single transaction: track min price and max profit in one pass.

How do I know if it's one transaction or multiple?+

The problem statement will explicitly say 'buy and sell once' or 'as many times as you want.' Read the constraints first. If it's vague during the OA, assume multiple transactions unless told otherwise. That's the harder version and proves you can code both.

What's the time complexity I need to hit?+

Linear time, one pass. That's non-negotiable for a greedy problem. If your solution loops twice or uses DP, you're overcomplicating it. Single array scan. Space is O(1) or O(n) depending on whether they ask for the transactions themselves or just the profit number.

Are there edge cases that trip people up on IBM's version?+

Empty array, single element, all prices declining (profit is zero, not negative), and huge price swings. Also: if multiple transactions are allowed, make sure you're not subtracting fees twice. Read the fine print. IBM sometimes adds twists like transaction fees or cooldown periods.

Can I solve this in 15 minutes if I've never seen it?+

Yes, if you recognize the greedy pattern. Multi-transaction: iterate and sum upward deltas. Single transaction: one loop tracking min and max. Write it, trace through one example, done. The pattern is standard. Don't overthink it under time pressure.

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

OA at IBM?
Invisible during screen share
Get it