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 Go
package main import "fmt" func towerOfHanoi(n int, from string, to string, aux string) { if n == 1 { fmt.Printf("Move disk 1 from %s to %s\n", from, to) return } towerOfHanoi(n-1, from, aux, to) fmt.Printf("Move disk %d from %s to %s\n", n, from, to) towerOfHanoi(n-1, aux, to, from) } func main() { towerOfHanoi(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
For 2 disks, the moves are: move disk 1 from A to B, move disk 2 from A to C, then move disk 1 from B to C.
🧠 Conceptual
intermediateMinimum moves required for Tower of Hanoi with 4 disks
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.
🔧 Debug
advancedIdentify the error in this Tower of Hanoi code snippet
What error will this Go code produce when run?
DSA Go
package main import "fmt" func towerOfHanoi(n int, from string, to string, aux string) { if n == 0 { fmt.Printf("Move disk 0 from %s to %s\n", from, to) return } towerOfHanoi(n-1, from, aux, to) fmt.Printf("Move disk %d from %s to %s\n", n, from, to) towerOfHanoi(n-1, aux, to, from) } func main() { 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 handles n==0 by printing a move for disk 0, which does not exist. This is logically incorrect but does not cause runtime error.
🚀 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
Total moves for 5 disks is 2^5 - 1. Subtract moves done.
✗ Incorrect
Total moves for 5 disks = 2^5 - 1 = 31. Remaining moves = 31 - 10 = 21.
❓ Predict Output
expertOutput of Tower of Hanoi moves for 3 disks with custom peg names
What is the output of the Tower of Hanoi function call for 3 disks with pegs named X, Y, Z?
DSA Go
package main import "fmt" func towerOfHanoi(n int, from string, to string, aux string) { if n == 1 { fmt.Printf("Move disk 1 from %s to %s\n", from, to) return } towerOfHanoi(n-1, from, aux, to) fmt.Printf("Move disk %d from %s to %s\n", n, from, to) towerOfHanoi(n-1, aux, to, from) } func main() { towerOfHanoi(3, "X", "Z", "Y") }
Attempts:
2 left
💡 Hint
Follow the recursive pattern carefully with the given peg names.
✗ Incorrect
The correct sequence follows the classic Tower of Hanoi moves with pegs X, Y, Z as source, auxiliary, and target respectively.
