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 f(Integer m, Integer n)
    calls = calls + 1
    if (m == 0 OR n == 0)
        return 1
    end if
    return f(m - 1, n) + f(m, n - 1)
End Function
Print f(3, 3), calls
  1. A. 20 20
  2. B. 20 39
  3. C. 6 20
  4. D. 20 19

Correct answer: B. 20 39

f(m, n) counts lattice paths, so f(3, 3) = C(6, 3) = 20. Every call either returns 1 (a leaf) or makes exactly two calls; the value 20 is the number of leaves, and a tree like this has one fewer inner call than leaves, so calls = 20 + 19 = 39.

More Pseudocode and Code Tracing questions

Exams that ask this topic