Data Structures & AlgorithmsHardProduct Company SDE · segment tree
Problem: Which data structure answers range-GCD queries with point updates in O(log n) each?
- A. A stack
- B. A hash set
- C. A linked list
- D. A segment tree storing gcd of each node's range
Correct answer: D. A segment tree storing gcd of each node's range
gcd is associative, so each node stores the gcd of its range and queries combine O(log n) nodes.
More Coding Interview Problems questions
- Problem: What is the complexity of computing the n-th Fibonacci number modulo a prime p using matrix exponentiation?
- Problem: Which technique counts pairs in an array whose XOR is less than K in O(n bits) time?
- Problem: What is the time complexity of the Wagner-Fischer dynamic programme for edit distance on strings of length n and m?
- Problem: Which two properties define a matroid, which is what makes the greedy algorithm optimal?
- Problem: Which algorithm finds shortest paths in a grid where each cell weight is 0 or 1?
- Problem: Which technique counts subarrays with sum at most K in a non-negative array in O(n)?