Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) on “Towers of Hanoi using Recursion”.
1. What is the objective of tower of hanoi puzzle?
a) To move all disks to some other rod by following rules
b) To divide the disks equally among the three rods by following rules
c) To move all disks to some other rod in random order
d) To divide the disks equally among three rods in random order
Answer: a
Clarification: Objective of tower of hanoi problem is to move all disks to some other rod by following the following rules-1) Only one disk can be moved at a time. 2) Disk can only be moved if it is the uppermost disk of the stack. 3) No disk should be placed over a smaller disk.
2. Which of the following is NOT a rule of tower of hanoi puzzle?
a) No disk should be placed over a smaller disk
b) Disk can only be moved if it is the uppermost disk of the stack
c) No disk should be placed over a larger disk
d) Only one disk can be moved at a time
Answer: c
Clarification: The rule is to not put a disk over a smaller one. Putting a smaller disk over larger one is allowed.
3. The time complexity of the solution tower of hanoi problem using recursion is _________
a) O(n2)
b) O(2n)
c) O(n log n)
d) O(n)
Answer: b
Clarification: Time complexity of the problem can be found out by solving the recurrence relation: T(n)=2T(n-1)+c. Result of this relation is found to be equal to 2n. It can be solved using substitution.
4. Recurrence equation formed for the tower of hanoi problem is given by _________ Answer: c 5. Minimum number of moves required to solve a tower of hanoi problem with n disks is __________ Answer: b 6. Space complexity of recursive solution of tower of hanoi puzzle is ________ 7. Which of the following functions correctly represent the solution to tower of hanoi puzzle? b) c) d) Answer: a 8. Recursive solution of tower of hanoi problem is an example of which of the following algorithm? Answer: d 9. Tower of hanoi problem can be solved iteratively. Answer: a 10. Minimum time required to solve tower of hanoi puzzle with 4 disks assuming one move takes 2 seconds, will be __________ Answer: b & Algorithms.
a) T(n) = 2T(n-1)+n
b) T(n) = 2T(n/2)+c
c) T(n) = 2T(n-1)+c
d) T(n) = 2T(n/2)+n
Clarification: As there are 2 recursive calls to n-1 disks and one constant time operation so the recurrence relation will be given by T(n) = 2T(n-1)+c.
a) 2n
b) 2n-1
c) n2
d) n2-1
Clarification: Minimum number of moves can be calculated by solving the recurrence relation – T(n)=2T(n-1)+c. Alternatively we can observe the pattern formed by the series of number of moves 1,3,7,15…..Either way it turn out to be equal to 2n-1.
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b
Clarification: Space complexity of tower of hanoi problem can be found out by solving the recurrence relation T(n)=T(n-1)+c. Result of this relation is found out to be n. It can be solved using substitution.
a)void ToH(int n,int a,int b,int c)
{
If(n>0)
{
ToH(n-1,a,c,b);
cout<<”move a disk from” <<a<<” to”<< c;
ToH(n-1,b,a,c);
}
}
void ToH(int n,int a,int b,int c
{
If(n>0)
{
ToH(n-1,a,b,c);
cout<<”move a disk from” <<a<<” to”<< c;
ToH(n-1,b,a,c);
}
}
void ToH(int n,int a,int b,int c)
{
If(n>0)
{
ToH(n-1,a,c,b);
cout<<”move a disk from” <<a<<” to”<< c;
ToH(n-1,a,b,c);
}
}
void ToH(int n,int a,int b,int c)
{
If(n>0)
{
ToH(n-1,b,a,c);
cout<<”move a disk from” <<a<<” to”<< c;
ToH(n-1,a,c,b);
}
}
Clarification: The first recursive call moves n-1 disks from a to b using c. Then we move a disk from a to c. Finally the second recursive call moves n-1 disks from b to c using a.
a) Dynamic programming
b) Backtracking
c) Greedy algorithm
d) Divide and conquer
Clarification: The recursive approach uses divide and conquer algorithm as we break the problem into smaller parts and then solve the smaller parts and finally combine their results to get the overall solution.
a) True
b) False
Clarification: Iterative solution to tower of hanoi puzzle also exists. Its approach depends on whether the total numbers of disks are even or odd.
a) 15 seconds
b) 30 seconds
c) 16 seconds
d) 32 seconds
Clarification: Number of moves = 24-1=16-1=15
Time for 1 move = 2 seconds
Time for 15 moves = 15×2 = 30 seconds.