Quant Memo
Coding/●●●●●

Best contiguous run of daily P&L

Asked at Two Sigma

You are given a strategy's daily profit and loss as a list (values can be negative). You want the single contiguous stretch of days that would have made the most money, holding the position from some start day through some end day. You must be in the market at least one day.

pnl = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
-> 6           # the run [4, -1, 2, 1]

pnl = [-3, -1, -2]
-> -1          # every day loses; the least-bad single day

Return the maximum contiguous sum. Aim for O(n)O(n) time and O(1)O(1) space.

Your answer

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

More Coding questions