ProgrammingVery hardGoldman Sachs OA · Amazon OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer move = 0
Procedure hanoi(Integer n, Char a, Char b, Char c)
    if (n == 0)
        return
    end if
    hanoi(n - 1, a, c, b)
    move = move + 1
    if (move == 12)
        Print n, a, c
    end if
    hanoi(n - 1, b, a, c)
End Procedure
hanoi(4, 'A', 'B', 'C')
  1. A. 3 B C
  2. B. 2 A C
  3. C. 3 B A
  4. D. 4 A C

Correct answer: A. 3 B C

In the Tower of Hanoi, move m moves disk (number of trailing zeros of m) + 1; 12 = 1100 in binary, so disk 3 moves. Tracing the recursion, move 12 is disk 3 going from B to C: 3 B C.

More Pseudocode and Code Tracing questions

Exams that ask this topic