0
0
DSA Typescriptprogramming~20 mins

Tower of Hanoi Problem in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tower of Hanoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'));
A
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
B
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
C
Move disk 2 from A to C
Move disk 1 from A to B
Move disk 1 from B to C
D
Move disk 1 from B to C
Move disk 2 from A to C
Move disk 1 from A to B
Attempts:
2 left
💡 Hint
Remember the recursive steps: move n-1 disks to auxiliary, move largest disk, then move n-1 disks to target.
🧠 Conceptual
intermediate
1: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?
A8
B31
C15
D16
Attempts:
2 left
💡 Hint
The minimum moves follow the formula 2^n - 1 where n is the number of disks.
🔧 Debug
advanced
1: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');
ANo error, outputs correct moves
BStack overflow error due to infinite recursion
CTypeError because of wrong parameter types
DSyntaxError due to missing return statement
Attempts:
2 left
💡 Hint
Check the base case and recursive calls carefully.
Predict Output
advanced
1: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);
A6
B8
C9
D7
Attempts:
2 left
💡 Hint
Minimum moves for n disks is 2^n - 1.
🚀 Application
expert
1: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?
Afunction minMoves(n: number): number { return Math.pow(2, n) - 1; }
Bfunction minMoves(n: number): number { return 2 * n + 1; }
Cfunction minMoves(n: number): number { return n * n - 1; }
Dfunction minMoves(n: number): number { return n * 2 - 1; }
Attempts:
2 left
💡 Hint
Recall the formula for minimum moves is 2^n - 1.