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 printed sequence of moves when solving Tower of Hanoi with 2 disks from peg A to peg C using peg B as auxiliary?
DSA Rust
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) { if n == 1 { println!("Move disk 1 from {} to {}", from, to); return; } tower_of_hanoi(n - 1, from, aux, to); println!("Move disk {} from {} to {}", n, from, to); tower_of_hanoi(n - 1, aux, to, from); } fn main() { tower_of_hanoi(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 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 with 4 disks
How many minimum moves are 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.
❓ Predict Output
advancedOutput of Tower of Hanoi moves for 3 disks with peg names X, Y, Z
What is the printed sequence of moves when solving Tower of Hanoi with 3 disks from peg X to peg Z using peg Y as auxiliary?
DSA Rust
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) { if n == 1 { println!("Move disk 1 from {} to {}", from, to); return; } tower_of_hanoi(n - 1, from, aux, to); println!("Move disk {} from {} to {}", n, from, to); tower_of_hanoi(n - 1, aux, to, from); } fn main() { tower_of_hanoi(3, 'X', 'Z', 'Y'); }
Attempts:
2 left
💡 Hint
Follow the recursive pattern carefully for 3 disks.
✗ Incorrect
The correct sequence follows the recursive steps moving n-1 disks to auxiliary, largest disk to target, then n-1 disks to target.
🔧 Debug
advancedIdentify the error in Tower of Hanoi recursive function
What error will this Rust code produce when running the Tower of Hanoi function with n=3?
fn tower_of_hanoi(n: u32, from: char, to: char, aux: char) {
if n == 0 {
println!("Move disk 1 from {} to {}", from, to);
}
tower_of_hanoi(n - 1, from, aux, to);
println!("Move disk {} from {} to {}", n, from, to);
tower_of_hanoi(n - 1, aux, to, from);
}
fn main() {
tower_of_hanoi(3, 'A', 'C', 'B');
}
Attempts:
2 left
💡 Hint
Check the base case condition for recursion termination.
✗ Incorrect
The base case checks for n == 0 but prints move for disk 1, causing infinite recursion when n reaches 0.
🧠 Conceptual
expertNumber of moves for Tower of Hanoi with 10 disks
What is the minimum number of moves required to solve the Tower of Hanoi problem with 10 disks?
Attempts:
2 left
💡 Hint
Use the formula 2^n - 1 for n disks.
✗ Incorrect
For 10 disks, minimum moves = 2^10 - 1 = 1024 - 1 = 1023.
