Data Structures & AlgorithmsModerateCodeforces Div2 C · data structures

Problem: Which statement about building a segment tree over an array of n elements is correct?

  1. A. It always needs O(n log n) time because each level is merged with sorting
  2. B. It needs O(n^2) time because every node stores a copy of its range
  3. C. It can be built in O(1) time when queries are offline
  4. D. It can be built bottom-up in O(n) time for any associative combine operation

Correct answer: D. It can be built bottom-up in O(n) time for any associative combine operation

Each internal node is combined from its two children exactly once, and there are fewer than n internal nodes, so the build is O(n). Sorting at each level is an unnecessary cost that the trap suggests.

More Coding Interview Problems questions

Exams that ask this topic