ProgrammingHardAmazon OA · TCS Digital

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function ways(Integer n)
    if (n < 0)
        return 0
    end if
    if (n == 0)
        return 1
    end if
    return ways(n - 1) + ways(n - 2) + ways(n - 3)
End Function
Print ways(6)
  1. A. 13
  2. B. 24
  3. C. 44
  4. D. 32

Correct answer: B. 24

ways(n) counts ways to climb n steps taking 1, 2 or 3 at a time: 1, 1, 2, 4, 7, 13, 24 for n = 0 to 6, so 24. With only 1- and 2-steps the answer would be 13.

More Pseudocode and Code Tracing questions

Exams that ask this topic