Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

Mo's algorithm answers q offline range queries on an array of n elements by sorting queries into √n-sized blocks of their left ends. If adding/removing one element costs O(1), what is the total time?

  1. A. O((n + q)·√n)
  2. B. O(q log n)
  3. C. O(n·q)
  4. D. O(n log n + q)

Correct answer: A. O((n + q)·√n)

Within a block the right pointer moves monotonically (O(n) per block, √n blocks → O(n√n)), and the left pointer moves at most O(√n) per query (O(q√n)). Sorting adds O(q log q).

More Coding Interview Problems questions

Exams that ask this topic