ProgrammingVery hardGoldman Sachs OA · TCS Digital

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 1
    end if
    return n - M(F(n - 1))
End Function
Function M(Integer n)
    if (n == 0)
        return 0
    end if
    return n - F(M(n - 1))
End Function
Print F(6), M(6)
  1. A. 4 3
  2. B. 4 4
  3. C. 3 4
  4. D. 3 3

Correct answer: B. 4 4

Build both tables upward: F = 1, 1, 2, 2, 3, 3, 4 and M = 0, 0, 1, 2, 2, 3, 4. For example F(6) = 6 − M(F(5)) = 6 − M(3) = 4 and M(6) = 6 − F(M(5)) = 6 − F(3) = 4. Output 4 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic