Given an array, implement sumRange(left, right), which returns the sum of the elements from left to right inclusive. It will be called many times.
Before you code
Summing on every call is O(n). Build the prefix sums once in the constructor.
Hints
Phas length n + 1 withP[0] = 0.sumRange(l, r) = P[r + 1] - P[l].
Production & complexity notes
When solving Range sum query: immutable on LeetCode (easy), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Prefix sums to reinforce the underlying pattern.
What it practices
The prefix-sum array and its off-by-one convention, from Prefix sums.