Challenge - 5 Problems
Tower of Hanoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Tower of Hanoi moves for 2 disks
What is the output of the following Tower of Hanoi function call for 2 disks?
DSA Typescript
function towerOfHanoi(n: number, fromRod: string, toRod: string, auxRod: string, moves: string[] = []): string[] {
if (n === 1) {
moves.push(`Move disk 1 from ${fromRod} to ${toRod}`);
return moves;
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod, moves);
moves.push(`Move disk ${n} from ${fromRod} to ${toRod}`);
towerOfHanoi(n - 1, auxRod, toRod, fromRod, moves);
return moves;
}
const result = towerOfHanoi(2, 'A', 'C', 'B');
console.log(result.join('\n'));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
The Tower of Hanoi for 2 disks moves disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.
🧠 Conceptual
intermediate1:00remaining
Minimum moves required for Tower of Hanoi
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
advanced1:30remaining
Identify the error in Tower of Hanoi implementation
What error will this Tower of Hanoi code produce when called with n=3?
DSA Typescript
function towerOfHanoi(n: number, fromRod: string, toRod: string, auxRod: string): void {
if (n === 0) {
return;
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod);
console.log(`Move disk ${n} from ${fromRod} to ${toRod}`);
towerOfHanoi(n - 1, auxRod, toRod, fromRod);
}
towerOfHanoi(3, 'A', 'C', 'B');Attempts:
2 left
💡 Hint
Check the base case and recursive calls carefully.
✗ Incorrect
The base case n===0 stops recursion correctly, so no error occurs and moves print properly.
❓ Predict Output
advanced1:00remaining
Number of moves printed for 3 disks
How many moves will be printed by this Tower of Hanoi function call with 3 disks?
DSA Typescript
function towerOfHanoi(n: number, fromRod: string, toRod: string, auxRod: string, moves: string[] = []): string[] {
if (n === 1) {
moves.push(`Move disk 1 from ${fromRod} to ${toRod}`);
return moves;
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod, moves);
moves.push(`Move disk ${n} from ${fromRod} to ${toRod}`);
towerOfHanoi(n - 1, auxRod, toRod, fromRod, moves);
return moves;
}
const result = towerOfHanoi(3, 'A', 'C', 'B');
console.log(result.length);Attempts:
2 left
💡 Hint
Minimum moves for n disks is 2^n - 1.
✗ Incorrect
For 3 disks, minimum moves = 2^3 - 1 = 7 moves.
🚀 Application
expert1:30remaining
Calculate total moves for n disks using formula
Which TypeScript function correctly calculates the minimum number of moves required to solve Tower of Hanoi for n disks?
Attempts:
2 left
💡 Hint
Recall the formula for minimum moves is 2^n - 1.
✗ Incorrect
Option A correctly implements the formula 2^n - 1 for minimum moves.