Maximum Stock Profit With Time Gap
Reported by candidates from Toast's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that makes this Toast OA tricky is the 5-second gap. Buy at index i, sell at index j, and j - i has to be at least 5. Toast candidates reported this one in January 2026. It's the classic one-trade stock problem with a constraint bolted on, and that constraint breaks the usual single-pass min tracking. If you've seen Best Time to Buy and Sell Stock, you're 80% there. The last 20% is where people lose points. StealthCoder sits invisibly on your screen as a safety net if you blank on the live OA, but the pattern is simple enough to own before you start.
The problem
You are given the stock prices for one day in chronological order. Each element represents the price at one second. You may buy once and sell once. The sell time must be at least 5 seconds after the buy time. In other words, if you buy at index i and sell at index j, then j - i >= 5. Return the maximum profit possible. If no profitable trade exists, return 0. Function maximumProfitWithTimeGap(prices: long[]) → long Examples Example 1 prices = [10,8,7,12,9,15] return = 5 Buy at index 0 for 10 and sell at index 5 for 15. Buying at index 2 and selling at index 5 is not allowed because the gap is only 3 seconds. Example 2 prices = [5,4,3,2,1,10] return = 5 The best valid trade buys at index 0 and sells at index 5. Example 3 prices = [9,8,7,6,5,4] return = 0 No valid buy/sell pair gives a positive profit. Constraints prices.length >= 1 Each adjacent pair of prices is one second apart.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a lagged running minimum. Walk j from 5 to n-1. Before evaluating j, make sure the minimum covers indices 0 through j-5. So at each j, fold prices[j-5] into minSoFar, then compute prices[j] - minSoFar and update the best. Start best at 0 so a losing series returns 0, like Example 3. That's O(n) time and O(1) space. The common pitfall is updating the min with prices[j-1], which lets in trades with a gap smaller than 5. Example 1 catches this: buying at index 2 for 7 and selling at 15 looks like 8 profit but is illegal. Also handle arrays shorter than 6 by returning 0 immediately. Use long for the values, since the signature says long. If you freeze under the clock, StealthCoder can hand you this loop live, but the lagged min is the whole idea.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Maximum Stock Profit With Time Gap 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 StealthCoderYou've seen the question.
Make sure you actually pass Toast's OA.
Toast 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.
Maximum Stock Profit With Time Gap FAQ
What's the trick in the Toast maximum stock profit with time gap problem?+
Keep a running minimum that lags five indices behind your sell index. At each j, add prices[j-5] into the min, then check prices[j] minus that min. That guarantees j - i >= 5 without a nested loop, so it runs in linear time.
How hard is this OA question really?+
Easy to medium. It's the standard one-transaction stock problem with one twist. If you know the running-minimum approach, the only new work is shifting the min update by five indices. Most failures come from off-by-one errors, not from the algorithm.
What edge cases should I test?+
Test an array shorter than 6, which should return 0. Test a strictly decreasing array, which also returns 0. Test a case like Example 1, where the cheapest price is too close to the sell index. Also test exactly length 6, where only one pair (0 and 5) is valid.
Can I brute force it?+
A double loop over i and j with j >= i + 5 is correct but O(n^2). With no stated upper bound on length, assume large inputs and expect hidden tests to time out. Write the linear version. It's barely more code than the brute force.
How do I prepare for this in 48 hours?+
Redo the basic one-trade stock problem from memory, then add the lag. Write it twice in your OA language and run the three examples by hand. Practice the loop boundaries specifically: start j at 5 and add prices[j-5] to the min before computing profit.