Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: A recursive routine splits a problem of size n into parts n/3 and 2n/3 and does n extra work: W(n) = W(n/3) + W(2n/3) + n. The code evaluates W exactly and normalises by n·log{1.5} n. What is the growth?
Problem: A recursive routine splits a problem of size n into parts n/3 and 2n/3 and does n extra work: W(n) = W(n/3) + W(2n/3) + n. The code evaluates W exactly and normalises by n·log_{1.5} n. What is the growth?
import math
from functools import lru_cache
@lru_cache(None)
def W(n):
if n < 3: return n
return W(n // 3) + W(2 * n // 3) + n
print([round(W(n) / (n * math.log(n, 1.5)), 3) for n in (3**8, 3**10, 3**12)])The program prints [0.606, 0.609, 0.612]. Which conclusion is correct?- A. Θ(n) – the extra work per call is linear, so the total is linear
- B. Θ(n^1.5) – the 2n/3 branch dominates and the ratio would keep growing
- C. Θ(n log n) – the normalised ratio is essentially constant
- D. Θ(n²) – two recursive calls on parts of size Θ(n) square the work
Correct answer: C. Θ(n log n) – the normalised ratio is essentially constant
The normalised value stays almost constant (≈ 0.61), so W(n) = Θ(n log n). Intuition: every level of the recursion tree does at most n total work (the parts sum to n), and the deepest branch shrinks by 2/3 each level, giving about log_{1.5} n levels. The Master theorem does not apply to unequal splits; Akra–Bazzi (or this recursion-tree argument) gives Θ(n log n). A linear W would make the ratio fall like 1/log n.
More Coding Interview Problems questions
- Problem: Find the bug: Dijkstra (finalising a node when it is popped) is run on a graph with one negative edge and compared with…
- Problem: Count integers in [1, N] whose decimal digits have no two adjacent equal digits, using digit DP with states (position, previous…
- Problem: Count all valid topological orderings of a small DAG (n ≤ 20) with a DP over subsets: from a set of already placed nodes, place…
- Problem: A traffic monitor reports, for every window of k consecutive minutes, the busiest minute's count, and the dashboard shows the SUM…
- Problem: Find the maximum sum of a non-empty subarray of a CIRCULAR array (a subarray may wrap from the end to the start). python def…
- Problem: Install c routers at some of the given highway mile-markers so that the smallest distance between any two routers is as LARGE as…