Data Structures & AlgorithmsModerateProduct Company SDE · data structures
Problem: In a segment tree with lazy propagation, what is the cost of one range-add update on an array of length n?
- A. O(n), since lazy tags must reach every leaf
- B. O(log n), because only O(log n) nodes are visited and tagged
- C. O(sqrt n), using block decomposition
- D. O(1), since tags are never pushed
Correct answer: B. O(log n), because only O(log n) nodes are visited and tagged
A range update touches O(log n) canonical nodes, each marked with a lazy tag that is pushed down only when needed. Pushing to every leaf would be O(n) and is not required.
More Coding Interview Problems questions
- Problem: What does the following print? python print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))
- Problem: What does the following print? python print(bin(12 & -12), 12 & -12)
- Problem: Coins of values {1, 3, 4} are available, unlimited copies each. To make amount 6, the greedy algorithm always takes the largest…
- Problem: What does the following print? python def ed(a, b): m, n = len(a), len(b) dp = [[0] (n + 1) for in range(m + 1)] for i in range(m…
- Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?
- Problem: What does the following print? python a = list(range(10)) print(a[::-3], a[2:8:4])