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- A. 20 20
- B. 20 39
- C. 6 20
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer a = 1, b = 100, steps…
- What will be the output of the following pseudocode? Integer a[5] = {7, 7, 5, 7, 6} Integer m1 = -1, m2 = -1 for (each i from 0 to 4) if…
- What will be the output of the following pseudocode? Integer a[5] = {2, 3, 4, 5, 1}, passes = 0, n = 5 Boolean swapped for (each i from 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 77, steps = 0, p…
- What will be the output of the following pseudocode? (<< is left shift, | is bitwise OR, & is bitwise AND) Integer x = 0 for (each i from…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer mults = 0 Function…