What is the main goal of the Tower of Hanoi problem?
To move all disks from the source peg to the destination peg following the rules: only one disk can be moved at a time, and a larger disk cannot be placed on top of a smaller disk.
Click to reveal answer
beginner
What are the three pegs called in the Tower of Hanoi problem?
Source peg (where disks start), Auxiliary peg (used for temporary storage), and Destination peg (where disks must be moved).
Click to reveal answer
intermediate
Explain the recursive approach to solve the Tower of Hanoi problem.
Move n-1 disks from source to auxiliary peg, move the nth (largest) disk to destination peg, then move n-1 disks from auxiliary to destination peg recursively.
Click to reveal answer
intermediate
What is the minimum number of moves required to solve the Tower of Hanoi problem with n disks?
The minimum number of moves is 2^n - 1, where n is the number of disks.
Click to reveal answer
beginner
In C, what is the base case for the recursive Tower of Hanoi function?
When n (number of disks) is 1, move the disk directly from source to destination and return.
Click to reveal answer
What is the first step in the recursive solution of Tower of Hanoi for n disks?
AMove n-1 disks from auxiliary to destination peg
BMove the largest disk from source to destination peg
CMove all disks directly to destination peg
DMove n-1 disks from source to auxiliary peg
✗ Incorrect
The recursive solution starts by moving n-1 disks from the source peg to the auxiliary peg.
How many moves does it take to solve Tower of Hanoi with 3 disks?
A7
B5
C9
D3
✗ Incorrect
Minimum moves = 2^3 - 1 = 7 moves.
Which peg is used as temporary storage in Tower of Hanoi?
ASource peg
BDestination peg
CAuxiliary peg
DNone
✗ Incorrect
The auxiliary peg is used as temporary storage to help move disks.
What condition must be followed when placing disks on pegs?
ALarger disk can be placed on smaller disk
BOnly one disk can be moved at a time
CSmaller disk can be placed on larger disk
DDisks can be stacked in any order
✗ Incorrect
Only one disk can be moved at a time, and a larger disk cannot be placed on a smaller disk.
What is the base case in the Tower of Hanoi recursive function?
An = 1
Bn = 2
Cn = 0
Dn = 3
✗ Incorrect
When n = 1, move the disk directly and stop recursion.
Describe the steps to solve the Tower of Hanoi problem recursively for n disks.
Think about breaking the problem into smaller parts.
You got /3 concepts.
Explain why the minimum number of moves for Tower of Hanoi is 2^n - 1.
Consider how moves increase as disks increase.
You got /3 concepts.
Practice
(1/5)
1. What is the minimum number of moves required to solve the Tower of Hanoi problem with 3 disks?
easy
A. 6
B. 5
C. 9
D. 7
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 D
Quick Check:
2^3 - 1 = 7 [OK]
Hint: Use formula 2^n - 1 for minimum moves [OK]
Common Mistakes:
Using n instead of 2^n - 1
Calculating 2*n - 1 instead of 2^n - 1
Confusing number of disks with moves
2. Which of the following is the correct recursive function signature in C for solving Tower of Hanoi?
easy
A. void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod);
B. int towerOfHanoi(int n, char from_rod, char to_rod);
C. void towerOfHanoi(int n, int from_rod, int to_rod);
D. void towerOfHanoi(int n, char from_rod, char to_rod);
Solution
Step 1: Identify parameters needed for Tower of Hanoi
We need number of disks and names of three rods: source, destination, auxiliary.
Step 2: Check function signature matching these parameters
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod); has int n and three char rods, matching the problem requirements.
Final Answer:
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod); -> Option A
Quick Check:
Function needs 3 rods and disk count [OK]
Hint: Tower of Hanoi needs 3 rods as parameters [OK]
Common Mistakes:
Missing auxiliary rod parameter
Using int instead of char for rods
Returning int instead of void
3. What will be the output of the following C code snippet for Tower of Hanoi with 2 disks?
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main() {
towerOfHanoi(2, 'A', 'C', 'B');
return 0;
}
medium
A. Move disk 2 from rod A to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod B to rod C
B. Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
C. Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
D. Move disk 1 from rod B to rod C
Move disk 2 from rod A to rod C
Move disk 1 from rod A to rod B
Solution
Step 1: Trace recursive calls for n=2
First move disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.
Step 2: Match output with options
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C matches the exact sequence of moves printed by the code.
Final Answer:
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C -> Option C
Quick Check:
Recursive moves for 2 disks = Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C [OK]
Hint: For 2 disks, move smaller disk to aux first [OK]
Common Mistakes:
Swapping auxiliary and destination rods
Printing moves in wrong order
Confusing disk numbers
4. Identify the error in the following Tower of Hanoi C function and select the correct fix:
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 0) {
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
medium
A. Change base case condition to if (n == 1) instead of if (n == 0)
B. Add else before recursive calls
C. Change printf to print n-1 instead of n
D. Swap aux_rod and to_rod in recursive calls
Solution
Step 1: Analyze base case condition
Base case should stop recursion when n == 1, not 0, to print the last move.
Step 2: Understand effect of current base case
With n == 0, no moves are printed for the smallest disk, causing no output.
Final Answer:
Change base case condition to if (n == 1) instead of if (n == 0) -> Option A
Quick Check:
Base case must be n == 1 to print moves [OK]
Hint: Base case must be n == 1 to print last move [OK]
Common Mistakes:
Using n == 0 as base case
Changing print statement unnecessarily
Swapping rods incorrectly
5. You want to solve Tower of Hanoi for 4 disks but print only the moves where disk 1 is moved. Which modification to the recursive function is correct?
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
hard
A. Remove all recursive calls except for n == 1 case
B. Add condition to print only if n == 1 inside the function
C. Print all moves but filter disk 1 moves outside the function
D. Change base case to n == 0 and print only disk 1 moves
Solution
Step 1: Understand requirement to print only disk 1 moves
We must print moves only when disk number is 1 inside the recursive function.
Step 2: Modify print statements accordingly
Keep recursive calls intact but print only when n == 1, skipping other prints.
Final Answer:
Add condition to print only if n == 1 inside the function -> Option B
Quick Check:
Print condition inside recursion for disk 1 only [OK]
Hint: Print moves only when disk number is 1 inside recursion [OK]
Common Mistakes:
Removing recursive calls breaks solution
Filtering outside loses recursion context
Changing base case incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.