As reported: Toast senior software engineer loop (Q4 2025), round 1 (coding): given one day's stock prices as a list of longs, one per second and already in time order, return the maximum profit of the day from one buy and one sell, where the sell must be at least 5 seconds after the buy; return 0 if no profit is possible, as a long. Also reported as Toast's first coding round in July 2025, where brute force and the optimised solution took most of the time, with a follow-up: buy and sell any number of times, but a new buy is only allowed after the previous sell.
prices[i] is the price at second i. Choose a buy second i and a sell second j with j − i ≥ 5. Return the largest prices[j] − prices[i], or 0 if every such trade loses money or no such pair exists.
Example 1
prices = [10, 12, 8, 15, 9, 11, 20, 7]
Buy at second 0 (10), sell at second 6 (20): profit 10.
(Buying at 8 at second 2 would need a sale at second 7 or later: 7 — a loss.)
Output: 10
Example 2
prices = [5, 4, 3, 2, 1, 0, 1]
Output: 0
Constraints: 0 ≤ prices.length ≤ 10^5, 1 ≤ prices[i] ≤ 10^12.
Code runner format: one JSON array per line: prices. Implement maxProfit(prices), returning a 64-bit integer.
Follow-up: you may trade any number of times, holding at most one share — a new buy only after the previous sell. What is the maximum profit?