Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

Problem: A Fenwick tree (binary indexed tree) over positions 1–8 starts with all zeros. The operations are: add 5 at position 3, add 4 at position 7, add 2 at position 3, add −1 at position 5. What is the prefix sum of the first 6 positions?

  1. A. 10
  2. B. 4
  3. C. 7
  4. D. 6

Correct answer: D. 6

Position 3 holds 5 + 2 = 7 and position 5 holds −1, so the prefix sum of positions 1-6 is 7 − 1 = 6 (position 7 lies outside). Including position 7 gives 10, letting the second add overwrite position 3 gives 4, and forgetting the −1 gives 7.

More Coding Interview Problems questions

Exams that ask this topic