Support Dynamic Range Sums
MolocoSoftware Engineer · OnsiteLast updated
This question evaluates knowledge of data structures and algorithmic techniques for range queries and dynamic updates, along with complexity analysis for time and space.
Implement a data structure over an integer array nums.
Part 1: If the array never changes, support sumRange(left, right) that returns the sum of elements from index left to right, inclusive.
Part 2 (follow-up): Now the array is mutable. Support both:
update(index, value): setnums[index] = valuesumRange(left, right): return the inclusive range sum
Design the mutable version so that both operations are efficient when the array length and number of operations can each be up to 100,000. Be prepared to discuss multiple approaches and their tradeoffs.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.