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?

  1. A. O(log n) per query, after O(1) preprocessing
  2. B. O(n log n) per query
  3. C. O(1) per query, after O(n log n) preprocessing
  4. 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

Exams that ask this topic