Data Structures & AlgorithmsVery hardICPC regional · data structures
Problem: Which data structure supports adding a value to a prefix of an array and finding the first index whose value is at least x, each in O(log n)?
- A. A stack
- B. A hash map keyed by index
- C. A segment tree with lazy propagation storing range maxima
- D. A linked list
Correct answer: C. A segment tree with lazy propagation storing range maxima
Range-add lazy tags plus maxima in each node let a descent find the first index with value at least x in O(log n).
More Coding Interview Problems questions
- Problem: What is the time complexity of computing the diameter of a tree with n vertices by two breadth-first searches?
- Problem: How many integers in [1, 1000] are divisible by both 4 and 9? Run the code. python print(sum(1 for x in range(1, 1001) if x % 4…
- Problem: What is the sum of the decimal digits of 2^15? Run the code. python print(sum(int(c) for c in str(2 15))) What is printed?
- Problem: How many integers in [1, 100] are not perfect squares? Run the code. python print(sum(1 for x in range(1, 101) if int(round(x…
- Problem: How many inversions does the array [5, 1, 4, 2, 3] have? Run the code. python a = [5, 1, 4, 2, 3] print(sum(1 for i in…
- Problem: What is the greatest common divisor of 12, 18, 30 and 42? Run the code. python from math import gcd from functools import reduce…