Answer range sums with a prefix table
When many questions ask about slices of the same list, adding every slice repeats work. Store a running total before each position. A range then needs two lookups and one subtraction. Keep an initial zero so ranges beginning at index zero follow exactly the same rule as all other ranges.
values = [3, -2, 5]
prefix = [0, 3, 1, 6]
print(prefix[3] - prefix[1])Think before coding
The prefix at position i contains the sum strictly before i. Subtracting two prefixes cancels everything before left. Preparation is linear and each query is constant time. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write range_sums(values, queries). Each (left, right) uses the half-open slice values[left:right], with 0 <= left <= right <= len(values). Return one sum per query without modifying either input.
The explanation opens here once every check passes.