Quant Memo
Coding/●●●●●

Range PnL queries with prefix sums

You have a desk's daily PnL for the year and must serve many queries of the form "total PnL from day l through day r inclusive" (0-indexed).

pnl = [3, -1, 4, -2, 5]
query(1, 3) -> 1        # -1 + 4 - 2
query(0, 4) -> 9

Answer each query in O(1)O(1) after O(n)O(n) preprocessing. Summing the slice per query is O(n)O(n) per query, too slow for millions of queries.

Your answer

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

More Coding questions