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)
  1. A. 1024
  2. B. 1023
  3. C. 512
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic