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?
  1. A. Θ(n) – the extra work per call is linear, so the total is linear
  2. B. Θ(n^1.5) – the 2n/3 branch dominates and the ratio would keep growing
  3. C. Θ(n log n) – the normalised ratio is essentially constant
  4. 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

Exams that ask this topic