Tower of Hanoi Problem in DSA Java - Time & Space Complexity
We want to understand how the number of moves grows as we increase the number of disks in the Tower of Hanoi problem.
How does the work needed change when we add more disks?
Analyze the time complexity of the following code snippet.
public void towerOfHanoi(int n, char fromRod, char toRod, char auxRod) {
if (n == 1) {
System.out.println("Move disk 1 from " + fromRod + " to " + toRod);
return;
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod);
System.out.println("Move disk " + n + " from " + fromRod + " to " + toRod);
towerOfHanoi(n - 1, auxRod, toRod, fromRod);
}
This code moves n disks from one rod to another using recursion and an auxiliary rod.
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 and then adds one more move.
| Input Size (n) | Approx. Operations (moves) |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 7 |
| 4 | 15 |
| 5 | 31 |
Pattern observation: The moves grow exponentially, doubling with each added disk.
Time Complexity: O(2^n)
This means the number of moves grows very fast, doubling for each extra disk added.
[X] Wrong: "The time grows linearly with the number of disks because we just move each disk once."
[OK] Correct: Actually, disks are moved multiple times in a recursive pattern, so the total moves double with each added disk, not just increase by one.
Understanding this problem helps you think about recursion and exponential growth, which are common in many coding challenges.
"What if we changed the problem to move disks iteratively without 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]
- Using n instead of 2^n - 1
- Calculating 2*n - 1 instead of 2^n - 1
- Confusing number of disks with moves
Solution
Step 1: Identify base case for recursion
Base case stops recursion when no disks are left to move, i.e., n == 0.Step 2: Check each option
if (n == 0) return; correctly returns when n == 0, stopping recursion.Final Answer:
if (n == 0) return; -> Option CQuick Check:
Base case is n == 0 [OK]
- Using n == 1 as base case which causes extra calls
- Using negative values as base case
- Returning when n > 1 which stops too early
Solution
Step 1: Understand move sequence for 2 disks
Move smaller disk 1 from source A to auxiliary B.Step 2: Move larger disk 2 from source A to destination C, then move disk 1 from B to C
This completes the transfer following rules.Final Answer:
Move disk 1 from A to B\nMove disk 2 from A to C\nMove disk 1 from B to C -> Option AQuick Check:
Correct move order for 2 disks [OK]
- Swapping disk numbers in moves
- Moving larger disk before smaller disk
- Incorrect order of moves
void towerOfHanoi(int n, char from, char to, char aux) {
if (n == 1) {
System.out.println("Move disk 1 from " + from + " to " + to);
return;
}
towerOfHanoi(n - 1, from, aux, to);
System.out.println("Move disk " + n + " from " + from + " to " + to);
towerOfHanoi(n - 1, aux, to, from);
}Solution
Step 1: Analyze base case correctness
Base case n == 1 means move the single disk directly, which is correct.Step 2: Check recursive calls and print order
First move n-1 disks from source to auxiliary, then move nth disk, then move n-1 disks from auxiliary to destination. This matches the Tower of Hanoi logic.Final Answer:
There is no error; the function is correct -> Option BQuick Check:
Function matches Tower of Hanoi recursion [OK]
- Changing base case to n == 0 causing infinite recursion
- Swapping parameters in recursive calls
- Moving print statement incorrectly
Solution
Step 1: Calculate total moves for 4 disks
Total moves = 2^4 - 1 = 16 - 1 = 15 moves.Step 2: Determine first two moves in sequence
First move: Move smallest disk 1 from A to B (auxiliary). Second move: Move disk 2 from A to C (destination).Final Answer:
Total moves: 15\nFirst move: Move disk 1 from A to B\nSecond move: Move disk 2 from A to C -> Option AQuick Check:
2^4 - 1 = 15 and correct first moves [OK]
- Incorrect total moves calculation
- Swapping first two moves
- Confusing auxiliary and destination rods
Ready to prove it?
Pass the test and this pattern moves unknown
