ProgrammingVery hardProduct company OA · code-tracing level

What will be the output of the following pseudocode? (/ is integer division; calls counts every call, including the first.)

What will be the output of the following pseudocode? (/ is integer division; calls counts every call, including the first.)
Integer calls = 0
Function f(Integer n)
    calls = calls + 1
    if (n == 0)
        return 1
    end if
    return f(n / 2) + f(n / 3)
End Function

Print f(12), calls
  1. A. 12 12
  2. B. 7 13
  3. C. 13 23
  4. D. 12 23

Correct answer: D. 12 23

Build up: f(0) = 1, f(1) = f(0) + f(0) = 2, f(2) = f(1) + f(0) = 3, f(3) = f(1) + f(1) = 4, f(4) = f(2) + f(1) = 5, f(6) = f(3) + f(2) = 7, f(12) = f(6) + f(4) = 12. Calls follow C(0) = 1 and C(n) = 1 + C(n/2) + C(n/3): C(1) = 3, C(2) = 5, C(3) = 7, C(4) = 9, C(6) = 13, C(12) = 23. '7 13' are the values for f(6).

More Pseudocode and Code Tracing questions

Exams that ask this topic