ProgrammingHardTCS Digital · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function f(Integer n)
if (n == 0)
return 0
end if
if (n == 1)
return 1
end if
return 3 * f(n - 1) - 2 * f(n - 2)
End Function
Print f(10)- A. 1024
- B. 1023
- C. 512
- D. 59049
Correct answer: B. 1023
The values run 0, 1, 3, 7, 15, …: the recurrence f(n) = 3f(n − 1) − 2f(n − 2) has solution f(n) = 2ⁿ − 1, so f(10) = 1023.
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? (Arrays are indexed from 0) Integer a[8] = {2, 0, 1, 2, 1, 0, 2, 1} Integer lo = 0…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {7, 2, 9, 4, 1, 8, 5} Integer pivot =…
- What will be the output of the following pseudocode? Integer a[9] = {3, 3, 4, 2, 4, 4, 2, 4, 2} Integer cand = 0, cnt = 0 for (each i from…
- What will be the output of the following pseudocode? Integer a[7] = {16, 17, 4, 3, 5, 2, 1} Integer mx = a[6], c = 1 for (each i from 5…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[6] = {3, 5, 0, 1, 2, 4} Integer x = 0, steps…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {4, 5, 2, 25, 7, 8}, res[6] Stack st =…