Tower of Hanoi Problem in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to solve the Tower of Hanoi grows as we add more disks.
The question is: How does the number of moves increase when the number of disks increases?
Analyze the time complexity of the recursive Tower of Hanoi function below.
function towerOfHanoi(n: number, from: string, to: string, aux: string): void {
if (n === 1) {
console.log(`Move disk 1 from ${from} to ${to}`);
return;
}
towerOfHanoi(n - 1, from, aux, to);
console.log(`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.
Look at what repeats in the code:
- Primary operation: Recursive calls that move smaller stacks of disks.
- How many times: Each call makes two smaller recursive calls plus one move operation.
When we add one more 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 each time plus one extra move.
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 because we just move each disk once."
[OK] Correct: Each disk move depends on moving smaller stacks multiple times, so moves multiply, not just add up.
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 instead of recursively? 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 BQuick Check:
2^3 - 1 = 7 [OK]
- Forgetting to subtract 1
- Using n instead of 2^n
- Confusing moves with disks
Solution
Step 1: Identify parameters needed for Tower of Hanoi
We need number of disks (number), source peg (string), destination peg (string), and auxiliary peg (string).Step 2: Match with function signature
function towerOfHanoi(n: number, from: string, to: string, aux: string): void matches all parameters with correct types and returns void as it prints moves.Final Answer:
function towerOfHanoi(n: number, from: string, to: string, aux: string): void -> Option AQuick Check:
Correct parameters and return type [OK]
- Using wrong parameter types
- Missing auxiliary peg parameter
- Wrong return type
function towerOfHanoi(n: number, from: string, to: string, aux: string): void {
if (n === 1) {
console.log(`Move disk 1 from ${from} to ${to}`);
return;
}
towerOfHanoi(n - 1, from, aux, to);
console.log(`Move disk ${n} from ${from} to ${to}`);
towerOfHanoi(n - 1, aux, to, from);
}
towerOfHanoi(2, 'A', 'C', 'B');Solution
Step 1: Trace first recursive call with n=2
Call towerOfHanoi(1, 'A', 'B', 'C') prints "Move disk 1 from A to B".Step 2: Print move for disk 2
Prints "Move disk 2 from A to C".Step 3: Trace second recursive call with n=1
Call towerOfHanoi(1, 'B', 'C', 'A') prints "Move disk 1 from B to C".Final Answer:
Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C -> Option DQuick Check:
Follow recursion steps carefully [OK]
- Mixing source and auxiliary pegs
- Printing moves in wrong order
- Confusing disk numbers
function towerOfHanoi(n: number, from: string, to: string, aux: string): void {
if (n === 0) {
console.log(`Move disk 1 from ${from} to ${to}`);
return;
}
towerOfHanoi(n - 1, from, aux, to);
console.log(`Move disk ${n} from ${from} to ${to}`);
towerOfHanoi(n - 1, aux, to, from);
}Solution
Step 1: Analyze base case condition
Base case should be when n equals 1, not 0, because disk numbering starts at 1.Step 2: Fix base case and print statement
Change if (n === 0) to if (n === 1) and print move for disk 1.Final Answer:
Change base case to if (n === 1) and print move for disk 1 -> Option AQuick Check:
Base case must handle smallest disk [OK]
- Using n === 0 as base case
- Printing move for disk 1 in wrong place
- Skipping base case
Solution
Step 1: Understand requirement to print only disk 1 moves
We must keep recursion intact but print moves only when disk number is 1.Step 2: Modify print statement with condition
Add if (n === 1) before printing move to filter only disk 1 moves.Final Answer:
Add condition to print only if disk number is 1 inside the print statement -> Option CQuick Check:
Filter print, keep recursion [OK]
- Removing recursive calls breaks solution
- Filtering moves after recursion wastes time
- Changing base case incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
