Range Sum Query - Immutable
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given an integer array once, and then asked many questions of the form "what is the sum of the elements from index left to index right, inclusive?" The array never changes.
Implement:
- NumArray(nums) receives the array.
- sumRange(left, right) returns nums[left] + nums[left + 1] + ... + nums[right].
Indices are 0-based and always satisfy 0 <= left <= right < nums.length. Preprocess in O(n) so every query runs in O(1).
Examples
Constraints
- 1 <= nums.length <= 10^4
- -10^5 <= nums[i] <= 10^5
- 0 <= left <= right < nums.length
- At most 10^4 calls to `sumRange`.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.