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?
- A. O((n + q)·√n)
- B. O(q log n)
- C. O(n·q)
- 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
- Problem: Edges arrive one by one; report the first edge that creates an ODD cycle (making the graph non-bipartite), using union-find with…
- Problem: Total weight of a MAXIMUM spanning tree (Kruskal with edges sorted in decreasing order). python edges =…
- Find the bug: Dijkstra (finalising a node when first popped) is run on a graph with a NEGATIVE edge. Compare with Bellman–Ford. python…
- Problem: Topological order produced by DFS (append a node after all its descendants, then reverse). python g = {0: [2, 1], 1: [3], 2: [3…
- Problem: Maximum bipartite matching between workers (left) and jobs (right) with Kuhn's augmenting-path algorithm. python adj = {0: [0…
- Problem: Maximum flow from s = 0 to t = 5 with Edmonds–Karp (BFS augmenting paths). python from collections import deque cap = [[0]6 for…