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 C++
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { std::cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << std::endl; return; } towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); std::cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << std::endl; towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } int main() { towerOfHanoi(2, 'A', 'C', 'B'); return 0; }
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 solution for 2 disks involves moving disk 1 from A to B, then disk 2 from A to C, then 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 for 4 disks?
Attempts:
2 left
💡 Hint
The minimum moves follow the formula 2^n - 1 where n is the number of disks.
✗ Incorrect
For n disks, minimum moves = 2^n - 1. For 4 disks, 2^4 - 1 = 16 - 1 = 15.
🔧 Debug
advancedIdentify the error in Tower of Hanoi recursive function
What error will the following Tower of Hanoi function produce when called with n=3?
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 0) {
std::cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << std::endl;
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
std::cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << std::endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
Attempts:
2 left
💡 Hint
Check the base case condition for recursion termination.
✗ Incorrect
The base case checks for n == 0 but tries to move disk 1, so recursion never stops for n > 0 causing infinite recursion.
❓ Predict Output
advancedNumber of moves printed for 3 disks
How many moves will be printed by the Tower of Hanoi function when called with 3 disks?
DSA C++
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { std::cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << std::endl; return; } towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); std::cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << std::endl; towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } int main() { towerOfHanoi(3, 'A', 'C', 'B'); return 0; }
Attempts:
2 left
💡 Hint
Use the formula 2^n - 1 to find the number of moves.
✗ Incorrect
For 3 disks, minimum moves = 2^3 - 1 = 7 moves printed.
🚀 Application
expertTime complexity of Tower of Hanoi algorithm
What is the time complexity of the Tower of Hanoi recursive algorithm for n disks?
Attempts:
2 left
💡 Hint
Consider how the number of moves grows as n increases.
✗ Incorrect
The Tower of Hanoi algorithm requires 2^n - 1 moves, so time complexity is exponential O(2^n).
