Data Structures & AlgorithmsVery hardCAT · GATE CS
Problem: With O(n) preprocessing (Euler tour + sparse table for range-minimum query), LCA queries on a tree of n nodes can be answered in what time?
- A. O(log n) per query, after O(1) preprocessing
- B. O(n log n) per query
- C. O(1) per query, after O(n log n) preprocessing
- D. O(n) per query regardless of preprocessing
Correct answer: C. O(1) per query, after O(n log n) preprocessing
The Euler tour reduces LCA to a range-minimum query over an array of depths; a sparse table (built in O(n log n)) then answers each RMQ, and hence each LCA query, in O(1).
More Coding Interview Problems questions
- Problem: Which statement correctly describes the diameter of a tree (the longest path between any two nodes) and how to compute it…
- Problem: A min-heap (binary heap) supports insert and extract-min in what time complexity, and what is the time to build a heap from n…
- Problem: To find the k largest elements among n numbers, which approach is asymptotically best when k is much smaller than n?
- Problem: An LRU (Least Recently Used) cache with O(1) get and put operations is classically implemented with which combination of data…
- Problem: Which statement about Kruskal's algorithm's overall time complexity is correct for a graph with V vertices and E edges?
- Problem: Topological sort is only defined for which kind of graph, and what happens if Kahn's algorithm is run on a graph that violates…