ProgrammingHardTCS NQT · Accenture pattern

What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a function of n?

What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a function of n?
Function C(Integer n, Integer r)
    if (r == 0 or r == n)
        return 1
    end if
    return C(n - 1, r - 1) + C(n - 1, r)
End Function
  1. A. O(n)
  2. B. O(n log n)
  3. C. O(2^n)
  4. D. O(n^2)

Correct answer: C. O(2^n)

Without memoization, this naive recursive Pascal's-triangle computation branches into 2 calls at almost every step, recomputing many overlapping subproblems; its worst-case call count grows exponentially, O(2^n).

More Pseudocode and Code Tracing questions

Exams that ask this topic