Challenge - 5 Problems
Tower of Hanoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tower of Hanoi moves for 2 disks
What is the output of the following Tower of Hanoi function call for 2 disks?
DSA Python
def tower_of_hanoi(n, source, target, auxiliary): if n == 1: print(f"Move disk 1 from {source} to {target}") return tower_of_hanoi(n-1, source, auxiliary, target) print(f"Move disk {n} from {source} to {target}") tower_of_hanoi(n-1, auxiliary, target, source) tower_of_hanoi(2, 'A', 'C', 'B')
Attempts:
2 left
💡 Hint
Remember the recursive steps: move n-1 disks to auxiliary, move nth disk to target, then move n-1 disks from auxiliary to target.
✗ Incorrect
For 2 disks, the moves are: move disk 1 from source to auxiliary, move disk 2 from source to target, then move disk 1 from auxiliary to target.
🧠 Conceptual
intermediateMinimum moves required for Tower of Hanoi
What is the minimum number of moves required to solve the Tower of Hanoi problem with 4 disks?
Attempts:
2 left
💡 Hint
The minimum moves follow the formula 2^n - 1, where n is the number of disks.
✗ Incorrect
For 4 disks, minimum moves = 2^4 - 1 = 16 - 1 = 15.
❓ Predict Output
advancedOutput of Tower of Hanoi moves for 3 disks
What is the output of the following Tower of Hanoi function call for 3 disks?
DSA Python
def tower_of_hanoi(n, source, target, auxiliary): if n == 1: print(f"Move disk 1 from {source} to {target}") return tower_of_hanoi(n-1, source, auxiliary, target) print(f"Move disk {n} from {source} to {target}") tower_of_hanoi(n-1, auxiliary, target, source) tower_of_hanoi(3, 'A', 'C', 'B')
Attempts:
2 left
💡 Hint
Trace the recursive calls carefully for 3 disks.
✗ Incorrect
The correct sequence follows the recursive pattern moving n-1 disks to auxiliary, moving nth disk to target, then moving n-1 disks from auxiliary to target.
🔧 Debug
advancedIdentify the error in Tower of Hanoi code
What error will the following Tower of Hanoi code produce when called with n=2?
DSA Python
def tower_of_hanoi(n, source, target, auxiliary): if n == 1: print(f"Move disk 1 from {source} to {target}") return tower_of_hanoi(n-1, source, target, auxiliary) print(f"Move disk {n} from {source} to {target}") tower_of_hanoi(n-1, auxiliary, target, source) tower_of_hanoi(2, 'A', 'C', 'B')
Attempts:
2 left
💡 Hint
Check the order of parameters in recursive calls carefully.
✗ Incorrect
The recursive call tower_of_hanoi(n-1, source, target, auxiliary) is incorrect; it should be tower_of_hanoi(n-1, source, auxiliary, target). This causes infinite recursion.
🧠 Conceptual
expertTime complexity of Tower of Hanoi algorithm
What is the time complexity of the Tower of Hanoi recursive algorithm with n disks?
Attempts:
2 left
💡 Hint
Each move doubles the previous moves plus one more move.
✗ Incorrect
The Tower of Hanoi requires 2^n - 1 moves, so time complexity is exponential O(2^n).
