Data Structures & AlgorithmsModerateProduct Company SDE · data structures

Problem: For a segment tree with n leaves supporting range-sum queries and point updates, what is the total space needed in terms of n?

  1. A. O(n^2) because each node stores all prefix sums
  2. B. O(n), using about 2n to 4n nodes
  3. C. O(n log n), one level per query
  4. D. O(1), since only the root is stored

Correct answer: B. O(n), using about 2n to 4n nodes

A segment tree stores about 2n to 4n nodes, one per segment of the recursive decomposition, which is linear in n. Storing prefix sums per node is the O(n^2) trap.

More Coding Interview Problems questions

Exams that ask this topic