Discover how a simple rule can solve a seemingly impossible puzzle step-by-step!
Why Tower of Hanoi Problem in DSA Go?
Imagine you have three pegs and a stack of different sized disks on one peg. You want to move all disks to another peg, but you can only move one disk at a time and never place a bigger disk on top of a smaller one.
Trying to figure out the steps manually for many disks is confusing and takes a long time.
Manually planning each move for many disks is slow and easy to mess up. You might forget a step or break the rules by placing a bigger disk on a smaller one.
This makes the task frustrating and error-prone.
The Tower of Hanoi problem uses a simple set of rules and recursion to break the big problem into smaller ones. This way, the solution is clear, organized, and guaranteed to follow the rules.
It shows how a complex task can be solved step-by-step without confusion.
for i := 0; i < n; i++ { // manually move disks one by one // hard to track and error-prone }
func moveDisks(n int, from, to, aux string) { if n == 1 { fmt.Println("Move disk 1 from", from, "to", to) return } moveDisks(n-1, from, aux, to) fmt.Println("Move disk", n, "from", from, "to", to) moveDisks(n-1, aux, to, from) }
This concept enables solving complex, rule-based problems by breaking them into smaller, manageable steps automatically.
It is like organizing a stack of plates carefully from one shelf to another without breaking or mixing their order.
Manual planning is slow and error-prone for many disks.
Recursion breaks the problem into smaller, easy steps.
Ensures all moves follow the rules perfectly.
Practice
Solution
Step 1: Understand the formula for minimum moves
The minimum moves required for n disks is 2^n - 1.Step 2: Calculate for 3 disks
For 3 disks, minimum moves = 2^3 - 1 = 8 - 1 = 7.Final Answer:
7 -> Option DQuick Check:
2^3 - 1 = 7 [OK]
- Forgetting to subtract 1
- Using n instead of 2^n
- Confusing number of disks with moves
Solution
Step 1: Identify parameters needed for Tower of Hanoi
We need number of disks (int), and names of three rods (strings): from, to, auxiliary.Step 2: Match function signature
func towerOfHanoi(n int, from string, to string, aux string) matches with n as int and three string rods. Others miss parameters or have wrong types.Final Answer:
func towerOfHanoi(n int, from string, to string, aux string) -> Option BQuick Check:
Correct parameters and types = func towerOfHanoi(n int, from string, to string, aux string) [OK]
- Using wrong parameter types
- Missing auxiliary rod parameter
- Using fewer than 4 parameters
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")
}Solution
Step 1: Trace recursive calls for n=2
First move disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.Step 2: Match output with options
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C matches the exact sequence of moves printed by the code.Final Answer:
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C -> Option AQuick Check:
Correct move sequence for 2 disks = Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C [OK]
- Swapping auxiliary and target rods
- Printing moves in wrong order
- Confusing disk numbers
func towerOfHanoi(n int, from string, to string, aux string) {
if n == 0 {
return
}
towerOfHanoi(n-1, from, to, aux)
fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
towerOfHanoi(n-1, aux, to, from)
}Solution
Step 1: Analyze recursive calls and base case
Base case with n==0 is okay, but first recursive call uses wrong rod order: it should move disks from 'from' to 'aux' using 'to' as auxiliary.Step 2: Correct first recursive call
Swap 'to' and 'aux' in first recursive call to towerOfHanoi(n-1, from, aux, to).Final Answer:
Swap 'to' and 'aux' in first recursive call -> Option CQuick Check:
Correct rod order in recursion = Swap 'to' and 'aux' in first recursive call [OK]
- Confusing auxiliary and target rods
- Incorrect base case condition
- Wrong print statement parameters
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)
}Solution
Step 1: Understand when disk 1 moves are printed
Disk 1 moves are printed only in base case when n == 1.Step 2: Modify print statements to print only for disk 1
Add an if condition around print statements to print only when n == 1, skipping other disks.Final Answer:
Add an if condition to print only when n == 1 inside the function -> Option AQuick Check:
Print only disk 1 moves = Add an if condition to print only when n == 1 inside the function [OK]
- Removing base case print
- Printing based on rod names instead of disk number
- Changing base case incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
