Maintain Inclusive Array Sums across Point Assignments

Quick Overview

Maintain Inclusive Array Sums across Point Assignments. Implement NumArray(nums), update(index,val) assigning the new value, and sumRange(left,right) returning the inclusive zero-based range sum. Calls share persiste

Maintain Inclusive Array Sums across Point Assignments

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement NumArray(nums), update(index,val) assigning the new value, and sumRange(left,right) returning the inclusive zero-based range sum. Calls share persistent updated state; preserve all original methods. ```cpp class NumArray { public: NumArray(vector<int>& nums); void update(int index, int val); int sumRange(int left, int right); }; ``` ### Examples ```text NumArray([2,4,6]); sumRange(0,2) -> 12; update(1,-1); sumRange(0,2) -> 7 NumArray([5]); update(0,0); sumRange(0,0) -> 0 ``` ### Constraints - Length is 1 through 30000. - Initial and assigned values lie in [-100,100]. - All update indices and inclusive query ranges are valid. - At most 30000 update/sumRange calls occur.

Overview: Maintain Inclusive Array Sums across Point Assignments. Implement NumArray(nums), update(index,val) assigning the new value, and sumRange(left,right) returning the inclusive zero-based range sum. Calls share persiste

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement NumArray(nums), update(index,val) assigning the new value, and sumRange(left,right) returning the inclusive zero-based range sum. Calls share persistent updated state; preserve all original methods.

class NumArray {
public:
    NumArray(vector<int>& nums);
    void update(int index, int val);
    int sumRange(int left, int right);
};

Examples

NumArray([2,4,6]); sumRange(0,2) -> 12; update(1,-1); sumRange(0,2) -> 7
NumArray([5]); update(0,0); sumRange(0,0) -> 0

Constraints

  • Length is 1 through 30000.
  • Initial and assigned values lie in [-100,100].
  • All update indices and inclusive query ranges are valid.
  • At most 30000 update/sumRange calls occur.