Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: A recursive function on n does O(1) work and makes two calls on n/2. What is T(n)?

  1. A. O(log n)
  2. B. O(n) – T(n) = 2T(n/2) + O(1)
  3. C. O(n log n)
  4. D. O(2ⁿ)

Correct answer: B. O(n) – T(n) = 2T(n/2) + O(1)

Master theorem: a = 2, b = 2, n^(log_b a) = n, and f(n) = O(1) is polynomially smaller, so T(n) = Θ(n). The tree has about 2n nodes each doing constant work. O(n log n) needs O(n) work per level, as in merge sort.

More Coding Interview Problems questions

Exams that ask this topic