Challenge - 5 Problems
Tower of Hanoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tower of Hanoi moves for 2 disks
What is the output of the following Tower of Hanoi function call for 2 disks?
DSA Javascript
function towerOfHanoi(n, fromRod, toRod, auxRod) {
if (n === 1) {
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`);
return;
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod);
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`);
towerOfHanoi(n - 1, auxRod, toRod, fromRod);
}
towerOfHanoi(2, 'A', 'C', 'B');Attempts:
2 left
💡 Hint
Remember the recursive steps: move n-1 disks to auxiliary rod, move nth disk to target rod, then move n-1 disks from auxiliary to target rod.
✗ Incorrect
The Tower of Hanoi solution 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
intermediateMinimum 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.
❓ Predict Output
advancedOutput of Tower of Hanoi moves for 3 disks with custom rods
What is the output of the following Tower of Hanoi function call for 3 disks with rods labeled X, Y, Z?
DSA Javascript
function towerOfHanoi(n, fromRod, toRod, auxRod) {
if (n === 1) {
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`);
return;
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod);
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`);
towerOfHanoi(n - 1, auxRod, toRod, fromRod);
}
towerOfHanoi(3, 'X', 'Z', 'Y');Attempts:
2 left
💡 Hint
Follow the recursive pattern carefully with the given rod names.
✗ Incorrect
The correct sequence for 3 disks moving from X to Z using Y as auxiliary is option B.
🔧 Debug
advancedIdentify the error in Tower of Hanoi implementation
What error will the following Tower of Hanoi code produce when called with n=2?
DSA Javascript
function towerOfHanoi(n, fromRod, toRod, auxRod) {
if (n === 1) {
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`)
return
}
towerOfHanoi(n - 1, fromRod, auxRod, toRod)
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`)
towerOfHanoi(n - 1, auxRod, toRod, fromRod)
}
towerOfHanoi(2, 'A', 'C', 'B')Attempts:
2 left
💡 Hint
JavaScript does not require semicolons strictly; check function logic.
✗ Incorrect
The code runs correctly and outputs the moves for 2 disks; missing semicolons do not cause errors here.
🧠 Conceptual
expertTime complexity of Tower of Hanoi algorithm
What is the time complexity of the Tower of Hanoi recursive algorithm with n disks?
Attempts:
2 left
💡 Hint
The number of moves doubles with each additional disk minus one.
✗ Incorrect
The Tower of Hanoi algorithm has exponential time complexity O(2^n) because the moves double with each disk added.
