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 code for 2 disks?
DSA C#
void TowerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { Console.WriteLine($"Move disk 1 from rod {from_rod} to rod {to_rod}"); return; } TowerOfHanoi(n - 1, from_rod, aux_rod, to_rod); Console.WriteLine($"Move disk {n} from rod {from_rod} to rod {to_rod}"); TowerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } TowerOfHanoi(2, 'A', 'C', 'B');
Attempts:
2 left
💡 Hint
Remember the recursive steps: move n-1 disks to auxiliary rod, move nth disk to target, then move n-1 disks from auxiliary to target.
✗ Incorrect
The Tower of Hanoi algorithm moves the top n-1 disks to the auxiliary rod, then moves the largest disk to the target rod, and finally moves the n-1 disks from auxiliary to target. For 2 disks, the moves are: disk 1 from A to B, disk 2 from A to C, disk 1 from B to C.
🧠 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
The minimum moves to solve Tower of Hanoi with n disks is 2^n - 1. For 4 disks, 2^4 - 1 = 16 - 1 = 15 moves.
🔧 Debug
advancedIdentify the error in Tower of Hanoi code
What error will this Tower of Hanoi code produce when run with n=3?
DSA C#
void TowerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 0) { Console.WriteLine($"Move disk {n} from rod {from_rod} to rod {to_rod}"); return; } TowerOfHanoi(n - 1, from_rod, aux_rod, to_rod); Console.WriteLine($"Move disk {n} from rod {from_rod} to rod {to_rod}"); TowerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } TowerOfHanoi(3, 'A', 'C', 'B');
Attempts:
2 left
💡 Hint
Check the base case condition and what happens when n=0.
✗ Incorrect
The base case incorrectly checks for n == 0 and prints a move for disk 0, which is invalid. The base case should be n == 1 to stop recursion properly.
🚀 Application
advancedNumber of moves for 5 disks after 10 moves
If you have solved 10 moves in the Tower of Hanoi problem with 5 disks, how many moves remain to complete the puzzle?
Attempts:
2 left
💡 Hint
Calculate total moves for 5 disks and subtract 10.
✗ Incorrect
Total moves for 5 disks is 2^5 - 1 = 31. After 10 moves, remaining moves = 31 - 10 = 21.
❓ Predict Output
expertOutput of Tower of Hanoi for 3 disks with custom rods
What is the output of this Tower of Hanoi code for 3 disks moving from rod X to rod Z using rod Y?
DSA C#
void TowerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { Console.WriteLine($"Move disk 1 from rod {from_rod} to rod {to_rod}"); return; } TowerOfHanoi(n - 1, from_rod, aux_rod, to_rod); Console.WriteLine($"Move disk {n} from rod {from_rod} to rod {to_rod}"); TowerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } TowerOfHanoi(3, 'X', 'Z', 'Y');
Attempts:
2 left
💡 Hint
Follow the recursive steps carefully with the given rod names.
✗ Incorrect
The Tower of Hanoi moves for 3 disks from X to Z using Y are: disk 1 from X to Z, disk 2 from X to Y, disk 1 from Z to Y, disk 3 from X to Z, disk 1 from Y to X, disk 2 from Y to Z, disk 1 from X to Z.
