Tower of Hanoi Problem in DSA Go - Time & Space Complexity
We want to understand how the time needed to solve the Tower of Hanoi grows as we add more disks.
How does the number of moves change when the number of disks increases?
Analyze the time complexity of the following Tower of Hanoi recursive solution.
func TowerOfHanoi(n int, from, to, aux string) {
if n == 1 {
fmt.Println("Move disk 1 from", from, "to", to)
return
}
TowerOfHanoi(n-1, from, aux, to)
fmt.Println("Move disk", n, "from", from, "to", to)
TowerOfHanoi(n-1, aux, to, from)
}
This code moves n disks from one peg to another using recursion and an auxiliary peg.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that move smaller stacks of disks.
- How many times: Each call makes two recursive calls with n-1 disks plus one move operation.
Each time we add a disk, the number of moves roughly doubles plus one extra move.
| Input Size (n) | Approx. Operations (Moves) |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 7 |
| 4 | 15 |
| 5 | 31 |
Pattern observation: The moves grow exponentially, doubling each time we add a disk.
Time Complexity: O(2^n)
This means the time needed doubles with each additional disk, growing very fast as n increases.
[X] Wrong: "The time grows linearly with the number of disks because we just move each disk once."
[OK] Correct: Each disk move depends on moving smaller stacks multiple times, so moves double with each added disk, not just increase by one.
Understanding this exponential growth helps you explain recursive problems clearly and shows you can analyze complex time patterns.
"What if we used an iterative approach instead of recursion? How would the time complexity change?"
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
