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 Java code when called with 2 disks?
DSA Java
public class TowerOfHanoi { public static void solve(int n, char from, char to, char aux) { if (n == 1) { System.out.println("Move disk 1 from " + from + " to " + to); return; } solve(n - 1, from, aux, to); System.out.println("Move disk " + n + " from " + from + " to " + to); solve(n - 1, aux, to, from); } public static void main(String[] args) { solve(2, 'A', 'C', 'B'); } }
Attempts:
2 left
💡 Hint
Remember the recursive steps: move n-1 disks to auxiliary, move largest disk, then move n-1 disks to target.
✗ Incorrect
The Tower of Hanoi solution for 2 disks moves disk 1 from source to auxiliary, then disk 2 from source to target, then disk 1 from auxiliary to target.
🧠 Conceptual
intermediateNumber of 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 formula for minimum moves is 2^n - 1, where n is the number of disks.
✗ Incorrect
For 4 disks, minimum moves = 2^4 - 1 = 16 - 1 = 15.
🔧 Debug
advancedIdentify the error in Tower of Hanoi code
What error will the following Java Tower of Hanoi code produce when run with n=3?
public static void solve(int n, char from, char to, char aux) {
if (n == 0) {
System.out.println("Move disk " + n + " from " + from + " to " + to);
return;
}
solve(n - 1, from, aux, to);
System.out.println("Move disk " + n + " from " + from + " to " + to);
solve(n - 1, aux, to, from);
}
Attempts:
2 left
💡 Hint
Check the base case condition and what it prints.
✗ Incorrect
The base case triggers when n == 0 and prints a move for disk 0, which is invalid since disks start from 1.
🚀 Application
advancedTrace Tower of Hanoi moves for 3 disks
What is the sequence of moves printed by the Tower of Hanoi algorithm for 3 disks moving from peg A to peg C using peg B as auxiliary?
Attempts:
2 left
💡 Hint
Follow the recursive pattern carefully: move n-1 disks to auxiliary, move largest disk, then move n-1 disks to target.
✗ Incorrect
The correct sequence follows the standard recursive solution for 3 disks moving from A to C using B.
🧠 Conceptual
expertTime complexity of Tower of Hanoi algorithm
What is the time complexity of the Tower of Hanoi recursive algorithm in terms of the number of disks n?
Attempts:
2 left
💡 Hint
Each move doubles the previous moves plus one more move for the largest disk.
✗ Incorrect
The recursive formula T(n) = 2*T(n-1) + 1 solves to O(2^n), exponential time complexity.
