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')- A. 3 B C
- B. 2 A C
- C. 3 B A
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) j = 1 while (j < i) j = j 2 end…
- What is the time complexity of the following pseudocode on an array of n elements? Stack st = empty for (each i from 0 to n - 1) while (st…
- What will be the output of the following pseudocode? (c ? x : y evaluates x if c is true, otherwise y) Integer marks[6] = {92, 75, 60, 88…
- What will be the output of the following pseudocode? (continue in a do-while loop goes straight to the while test) Integer x = 1, c = 0 do…
- What will be the output of the following pseudocode? (s[i] - '0' turns a digit character into its value; strings are indexed from 0)…
- What will be the output of the following pseudocode? (+ joins strings) Integer count = 0 String third = "" Procedure gen(String s, Integer…