Quant Memo
Coding/●●●●●

Max profit from a single trade

Asked at Citadel, Two Sigma

You're given the intraday price path of a stock as a list. You may buy once and sell once, and the sell must come after the buy. Short selling is not allowed; if no profitable trade exists, do nothing.

prices = [7, 1, 5, 3, 6, 4]
-> 5            # buy at 1, sell at 6

prices = [7, 6, 4, 3, 1]
-> 0            # prices only fall; skip the trade

Return the maximum achievable profit. Aim for O(n)O(n) time and O(1)O(1) space.

Show a hint

If you were forced to sell at time tt, when should you have bought?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions