Data Structures & AlgorithmsModerateCodeforces Div2 C · data structures
Problem: Which statement about building a segment tree over an array of n elements is correct?
- A. It always needs O(n log n) time because each level is merged with sorting
- B. It needs O(n^2) time because every node stores a copy of its range
- C. It can be built in O(1) time when queries are offline
- 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
- Problem: What is the maximum product of three numbers in [-10, -10, 5, 2]? Run the code. python a = sorted([-10, -10, 5, 2])…
- Problem: What is the maximum subarray sum over all prefixes of [-2, 1, -3, 4]? Print the list of running maximum-subarray values with…
- Array a = [1, -2, 3, 2, -1, 4, -3, 2]. What is the length of the longest contiguous subarray whose sum is exactly 4?
- Array a = [3, -5, 2, -1, -4, 2, 1, -6, 2]. How many contiguous subarrays of length exactly 4 have a negative sum?
- Sorted array [1, 2, 2, 3, 5, 5, 6, 8]. How many index pairs i < j have 6 <= a[i] + a[j] <= 8?
- How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7} have a bitwise AND of their elements equal to 0?