ProgrammingVery hardGoldman Sachs OA · Amazon OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer calls = 0
Function C(Integer n, Integer k)
    calls = calls + 1
    if (k == 0 OR k == n)
        return 1
    end if
    return C(n - 1, k - 1) + C(n - 1, k)
End Function
Print C(5, 2), calls
  1. A. 10 10
  2. B. 10 15
  3. C. 10 19
  4. D. 20 19

Correct answer: C. 10 19

The value is C(5, 2) = 10, built entirely from base cases that each return 1, so there are 10 leaf calls. Every other call makes exactly two calls, so there are 9 inner calls: 19 calls in all.

More Pseudocode and Code Tracing questions

Exams that ask this topic