14 · Hashing & Prefix Sums

#303 Range Sum Query - Immutable

Easysolve on LeetCode ↗

frequently reported atInfosysMetaAmazonBloombergGoogleMicrosoft

the problem

In plain words

Design a class built from an integer array that answers many sumRange(left, right) queries: the sum of elements between the two indices, inclusive. The array never changes.

in nums = [-2,0,3,-5,2,-1]; sumRange(0,2), sumRange(2,5), sumRange(0,5)

out 1, -1, -3

  • 1 ≤ n ≤ 10⁴
  • -10⁵ ≤ nums[i] ≤ 10⁵
  • Up to 10⁴ calls to sumRange

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #304
    2. #307
      Range Sum Query - Mutable
      Mediumin Range Queries & Strings
    3. #1480