Data Structures & AlgorithmsHardProduct-company MCQ pattern

What is the time complexity of g(n)?

What is the time complexity of g(n)?
def g(n):
    if n <= 1:
        return
    for i in range(n):
        pass        # O(1)
    g(n // 2)
    g(n // 2)
    g(n // 2)
  1. A. Θ(n^log₂3) ≈ Θ(n^1.585)
  2. B. Θ(n²)
  3. C. Θ(n log n)
  4. D. Θ(n)

Correct answer: A. Θ(n^log₂3) ≈ Θ(n^1.585)

T(n) = 3T(n/2) + n. Since n^(log₂ 3) ≈ n^1.585 grows faster than f(n) = n, case 1 of the master theorem gives Θ(n^log₂3). With two calls it would be Θ(n log n), the tempting answer.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic