Range Sum Query - Immutable Lab
In this lab, you will be working with the Range Sum Query problem. The goal is to implement the
NumArray class that handles multiple queries of the following type:
- Calculate the sum of the elements of
left <= right.
NumArray class should have two main methods:
NumArray(int nums)Initializes the object with the integer array
int sumRange(int left, int right)Returns the sum of the elements of
nums[left] + nums[left + 1] + ... + nums[right]).
import NumArray from './index.js'; const numArray = new NumArray([-2, 0, 3, -5, 2, -1]); console.log(numArray.sumRange(0, 2)); // return (-2) + 0 + 3 = 1 console.log(numArray.sumRange(2, 5)); // return 3 + (-5) + 2 + (-1) = -1 console.log(numArray.sumRange(0, 5)); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
1 <= nums.length <= 104
-105 <= nums[i] <= 105
0 <= left <= right < nums.length
- At most
104calls will be made to
In this lab, you will be given a few challenges to test your implementation of the
NumArray class. Make sure to export the
NumArray class and any additional variables or functions that may be required for testing in the challenges.
The challenges will check your implementation of the
NumArray class and the
sumRange method according to the given constraints. You will be provided with an evaluation script that will test your implementation against various test cases. Make sure to carefully read the instructions and follow the rules outlined above to build a proper lab environment.