Data Structures & AlgorithmsHardProduct Company SDE pattern

Solve the recurrences numerically: how many 'work units' does each do for n = 64?

Solve the recurrences numerically: how many 'work units' does each do for n = 64?
from functools import lru_cache
@lru_cache(None)
def A(n): return 1 if n <= 1 else 2 * A(n // 2) + n
@lru_cache(None)
def B(n): return 1 if n <= 1 else A_(n)
def A_(n): return B(n // 2) + 1
@lru_cache(None)
def Cc(n): return 1 if n <= 1 else 4 * Cc(n // 2) + n
print(A(64), B(64), Cc(64))
  1. A. 384 6 4096
  2. B. 448 6 8128
  3. C. 448 7 4096
  4. D. 448 7 8128

Correct answer: D. 448 7 8128

T(n) = 2T(n/2) + n with T(1) = 1 is n log₂ n + n = 64·6 + 64 = 448 → Θ(n log n). T(n) = T(n/2) + 1 is log₂ n + 1 = 7 → Θ(log n). T(n) = 4T(n/2) + n is 2n² − n = 8128 → Θ(n²) (master theorem case 1, since n^(log₂4) = n² dominates n).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic