Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

How many times is the base case reached when computing C(5, 2) with the plain recursion C(n, k) = C(n−1, k−1) + C(n−1, k)?

How many times is the base case reached when computing C(5, 2) with the plain recursion C(n, k) = C(n−1, k−1) + C(n−1, k)?
base = 0
def C(n, k):
    global base
    if k == 0 or k == n:
        base += 1; return 1
    return C(n - 1, k - 1) + C(n - 1, k)
print(C(5, 2), base)
  1. A. 10 10
  2. B. 10 5
  3. C. 10 20
  4. D. 6 10

Correct answer: A. 10 10

Every leaf of the recursion returns 1 and the answer is the sum of the leaves, so the number of base-case calls equals C(5, 2) = 10 itself – exponential growth for large n. Memoising on (n, k) or using Pascal's triangle makes it O(n·k).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic