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- A. 10 10
- B. 10 15
- C. 10 19
- 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.
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? // Integer16: 16-bit signed integers in two's complement; results wrap around…
- What will be the output of the following pseudocode? Record r[4] = {(3, 'a'), (1, 'b'), (3, 'c'), (1, 'd')} // (key, letter) for (each i…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {4, 2, 2, 8, 3, 3, 1}, cnt[0..8] = all 0…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {170, 45, 75, 90, 802, 24, 2, 66} List…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer lo = 1, hi = 1000…
- What will be the output of the following pseudocode? (^ is bitwise XOR) Integer x = 0 for (each i from 1 to 10) x = x ^ i end for Integer…