Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the move from source to destination.
DSA Java
System.out.println("Move disk from " + [1] + " to " + destination);
Attempts:
3 left
💡 Hint
Common Mistakes
Using destination instead of source.
Using a variable that does not represent the peg.
✗ Incorrect
The move is from the source peg to the destination peg, so we print the source variable.
2fill in blank
mediumComplete the code to call the Tower of Hanoi function recursively for n-1 disks.
DSA Java
towerOfHanoi([1] - 1, source, temp, destination);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of n-1.
Using peg names instead of number of disks.
✗ Incorrect
We reduce the number of disks by 1 for the recursive call.
3fill in blank
hardFix the error in the base case condition to stop recursion when no disks are left.
DSA Java
if ([1] == 0) return;
Attempts:
3 left
💡 Hint
Common Mistakes
Checking peg names instead of disk count.
Using wrong variable in condition.
✗ Incorrect
The base case checks if the number of disks n is zero to stop recursion.
4fill in blank
hardFill both blanks to correctly call the recursive steps before and after moving the largest disk.
DSA Java
towerOfHanoi([1] - 1, source, [2], destination); // move disk towerOfHanoi([1] - 1, [2], destination, source);
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up peg names in recursive calls.
Using wrong variable for disk count.
✗ Incorrect
We move n-1 disks using temp as auxiliary peg before and after moving the largest disk.
5fill in blank
hardFill all three blanks to complete the Tower of Hanoi method signature and recursive calls.
DSA Java
public static void towerOfHanoi(int [1], String [2], String [3], String destination) { if ([1] == 0) return; towerOfHanoi([1] - 1, [2], destination, [3]); System.out.println("Move disk from " + [2] + " to " + destination); towerOfHanoi([1] - 1, [3], [2], destination); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names.
Mixing peg names in recursive calls.
✗ Incorrect
The method takes number of disks n, source peg, and temp peg as parameters. Recursive calls use these variables correctly.
