TikTok VO Coding Interview OA: Sum and Update

20 Views
No Comments

You are given an array of integers. Implement a class that can support the following operations:

  • Get the sum of elements in a specific index range
  • Update one element to another value

For example:

Initial array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum(2, 5) = 18  # sum of 3, 4, 5, 6
update(3, -1)   # update the element at index 3 to -1
sum(2, 5) = 13

This problem asks you to design a data structure that supports range-sum queries and point updates on an integer array. The standard solution is to use a Fenwick tree or a segment tree, which keeps both operations efficient at logarithmic time. In the example, the sum of indices [2, 5] is first 18, and after updating index 3 to -1, the same range sum becomes 13.

END
 0