Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the move from source to destination.
DSA C++
void moveDisk(char [1], char dest) { std::cout << "Move disk from " << [1] << " to " << dest << std::endl; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the function parameters.
✗ Incorrect
The variable representing the source peg is named 'source' in the function parameter.
2fill in blank
mediumComplete the recursive call to move n-1 disks from source to auxiliary peg.
DSA C++
void towerOfHanoi(int n, char source, char dest, char aux) {
if (n == 1) {
moveDisk(source, dest);
return;
}
towerOfHanoi([1], source, aux, dest);
moveDisk(source, dest);
towerOfHanoi(n - 1, aux, dest, source);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n-1 in the recursive call.
Using n+1 which is invalid.
✗ Incorrect
The recursive call moves n-1 disks to the auxiliary peg before moving the largest disk.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA C++
void towerOfHanoi(int n, char source, char dest, char aux) {
if (n [1] 1) {
moveDisk(source, dest);
return;
}
towerOfHanoi(n - 1, source, aux, dest);
moveDisk(source, dest);
towerOfHanoi(n - 1, aux, dest, source);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' which causes incorrect recursion termination.
Using '!=' which never stops recursion properly.
✗ Incorrect
The base case is when n equals 1, so the condition should be 'n == 1'.
4fill in blank
hardFill both blanks to correctly call the recursive function for moving disks.
DSA C++
towerOfHanoi([1], source, aux, [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n-1.
Swapping source and destination pegs.
✗ Incorrect
The first argument is n-1 disks, and the last argument is the destination peg.
5fill in blank
hardFill all three blanks to complete the recursive calls and moveDisk call in the Tower of Hanoi function.
DSA C++
towerOfHanoi([1], source, aux, dest); moveDisk(source, [2]); towerOfHanoi([3], aux, dest, source);
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n-1 in recursive calls.
Swapping source and destination in moveDisk.
✗ Incorrect
The first and third recursive calls move n-1 disks, and moveDisk moves the largest disk from source to destination.
