Data Structures & AlgorithmsHardGoogle · Flipkart interview

An analytics service holds a static array of n = 10⁵ integers and must answer q = 10⁶ queries 'minimum of a[l..r]' as fast as possible. Which structure gives the best total time?

  1. A. Prefix-minimum array: O(n) build, O(1) per query
  2. B. Segment tree: O(n) build, O(log n) per query, which is optimal for static data
  3. C. Sqrt decomposition: O(n) build, O(1) per query
  4. D. Sparse table: O(n log n) build, O(1) per query using two overlapping power-of-two blocks

Correct answer: D. Sparse table: O(n log n) build, O(1) per query using two overlapping power-of-two blocks

Minimum is idempotent, so two overlapping blocks of length 2^k cover [l, r] and the sparse table answers in O(1): about 1.7 × 10⁶ build steps plus 10⁶ queries. Prefix minima only work for ranges starting at 0, and sqrt decomposition costs O(√n) per query; a segment tree works but adds a log factor.

More Coding Interview Problems questions

Exams that ask this topic