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?
- A. O(n^2) because each node stores all prefix sums
- B. O(n), using about 2n to 4n nodes
- C. O(n log n), one level per query
- 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
- Problem: Find the number of inversions in [5, 4, 3, 2, 1] that a merge-sort based counter would report. Run the code. python def count(a)…
- Problem: Which claim about the Knuth-Morris-Pratt algorithm is correct?
- Problem: A player picks a number from 1 to 10 and the other player must guess it by yes/no questions. The minimum worst-case number of…
- Problem: What is the complexity of computing all prefix XOR values of an array of n = 10^6 integers and then answering q = 10^6 range-XOR…
- Problem: Consider a DP over subsets for assigning n jobs to n workers (cost matrix given), with state = mask of assigned jobs and…
- Problem: A string of length n is built using the operation 'append the reverse of the current string'. Starting with 'a', the length after…