Data Structures & AlgorithmsHardProduct company OA

Problem: A divide-and-conquer routine makes 3 recursive calls on halves and does linear extra work: T(n) = 3T(n/2) + n. The code evaluates T exactly. What growth does the ratio T(2n)/T(n) reveal?

Problem: A divide-and-conquer routine makes 3 recursive calls on halves and does linear extra work: T(n) = 3T(n/2) + n. The code evaluates T exactly. What growth does the ratio T(2n)/T(n) reveal?
from functools import lru_cache
@lru_cache(None)
def T(n):
    if n <= 1: return 1
    return 3 * T(n // 2) + n
print(T(1024), T(2048), round(T(2048) / T(1024), 2))
What is printed?
  1. A. 11264 24576 2.18
  2. B. 175099 350198 2.0
  3. C. 175099 527345 2.0
  4. D. 175099 527345 3.01

Correct answer: D. 175099 527345 3.01

Master theorem: a = 3, b = 2, f(n) = n, and n^(log₂ 3) ≈ n^1.585 dominates n, so T(n) = Θ(n^log₂3). Doubling n multiplies the time by about 2^1.585 = 3 – the ratio 3.01 confirms it (an n log n algorithm would show ≈ 2.2, a linear one 2). This is the recurrence of Karatsuba multiplication.

More Coding Interview Problems questions

Exams that ask this topic