Reported September 2026
Salesforcegreedy

Maximum Stock Profit with Unlimited Transactions

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

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

The naive move on this Salesforce OA, reported in September 2026, is to hunt for one best buy day and one best sell day. That's the single-transaction version, and it fails Example 1 (you'd get 5, not 7). This one allows unlimited buy-then-sell trades with one share held at a time. It's a greedy array problem that looks harder than it is. If you've seen it, it's a five-line loop. If you blank, StealthCoder runs invisibly during the live OA and gives you the solution as a safety net. Either way, know the trick before the invite clock runs out.

The problem

Given prices, where prices[i] is one share's price on day i, return the maximum profit obtainable.
For this exercise, assume you may complete any number of buy-then-sell transactions, may hold at most one share, and must sell before buying again. You may also make no transaction.

Function
maxProfitUnlimited(prices: int[]) → long

Examples
Example 1
prices = [7,1,5,3,6,4]
return = 7
Buy at 1 and sell at 5, then buy at 3 and sell at 6.
Example 2
prices = [1,2,3,4,5]
return = 4
Holding across the increasing run earns 5 - 1 = 4.
Example 3
prices = [7,6,4,3,1]
return = 0
Every sale would lose money, so make no transaction.

Constraints
1 <= prices.length <= 10^5.
0 <= prices[i] <= 10^5.
The answer fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: every upward step between consecutive days is profit you can capture. Loop from day 1 and add prices[i] - prices[i-1] whenever it's positive. Holding across a long increasing run equals the sum of its daily gains, so Example 2 gives 1+1+1+1 = 4. Example 3 never adds anything, so you return 0. The pitfall is overthinking it with DP or trying to track explicit buy and sell days, which invites off-by-one bugs. The other pitfall is the single-element array: the loop doesn't run and you return 0, which is correct. Use a 64-bit accumulator because the return type is long. With 10^5 days and prices up to 10^5, the sum can pass 32-bit range. It's O(n) time and O(1) space. If your mind goes blank mid-assessment, StealthCoder is the hedge that surfaces this loop on screen without the proctor seeing it.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Maximum Stock Profit with Unlimited Transactions 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Stock Profit with Unlimited Transactions FAQ

How hard is this Salesforce stock profit problem really?+

Easy once you see it. The greedy solution is a single pass summing positive day-over-day differences. The difficulty is recognizing that unlimited transactions removes the need to pick specific buy and sell days. Most candidates who struggle are overcomplicating it with DP.

What's the trick to maxProfitUnlimited?+

Add prices[i] - prices[i-1] whenever it's positive. A long climb from 1 to 5 equals the sum of each daily rise, so you capture every gain without tracking transactions. Falling days contribute nothing because you simply don't hold through them.

Why does the single-transaction approach fail here?+

Tracking one min price and one max profit returns 5 on [7,1,5,3,6,4], but the answer is 7. Unlimited transactions let you capture both the 1-to-5 and 3-to-6 runs. Any approach capping you at one trade undercounts whenever prices dip and recover.

Do I need a long or big integer type?+

The signature returns long and the statement says the answer fits in signed 64-bit. With up to 10^5 days and prices up to 10^5, totals can exceed 32-bit range in some languages. Use a 64-bit accumulator to be safe.

How do I prepare for this in 48 hours?+

Write the greedy loop from memory twice, then test it on the three given examples plus a length-1 array. Also be ready to explain why it works, since follow-ups often ask for the DP version with states for holding and not holding.

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

OA at Salesforce?
Invisible during screen share
Get it