Data Structure Assessment Questions and Answers on “Minimum Insertions to form a Palindrome”.
1. Given a string, you have to find the minimum number of characters to be inserted in the string so that the string becomes a palindrome. Which of the following methods can be used to solve the problem?
a) Greedy algorithm
b) Recursion
c) Dynamic programming
d) Both recursion and dynamic programming
Answer: d
Clarification: Dynamic programming and recursion can be used to solve the problem.
2. In which of the following cases the minimum no of insertions to form palindrome is maximum?
a) String of length one
b) String with all same characters
c) Palindromic string
d) Non palindromic string
Answer: d
Clarification: In string of length one, string with all same characters and a palindromic string the no of insertions is zero since the strings are already palindromes. To convert a non-palindromic string to a palindromic string, the minimum length of string to be added is 1 which is greater than all the other above cases. Hence the minimum no of insertions to form palindrome is maximum in non-palindromic strings.
3. In the worst case, the minimum number of insertions to be made to convert the string into a palindrome is equal to the length of the string. Answer: b 4. Consider the string “efge”. What is the minimum number of insertions required to make the string a palindrome? Answer: b 5. Consider the string “abbccbba”. What is the minimum number of insertions required to make the string a palindrome? Answer: a 6. Which of the following problems can be used to solve the minimum number of insertions to form a palindrome problem? Answer: b 7. Consider the following dynamic programming implementation: Which of the following lines should be added to complete the code? Answer: d 8. What is the time complexity of the following dynamic programming implementation of the minimum number of insertions to form a palindrome problem? a) O(1) Answer: c 9. What is the space complexity of the following dynamic programming implementation of the minimum number of insertions to form a palindrome problem? a) O(1) Answer: c 10. What is the output of the following code? a) 1 Answer: b 11. What is the value stored in arr[2][4] when the following code is executed? a) 2 Answer: a 12. What is the output of the following code? a) 0 Answer: a & Algorithms. and Answers.
a) True
b) False
Clarification: In the worst case, the minimum number of insertions to be made to convert the string into a palindrome is equal to length of the string minus one. For example, consider the string “abc”. The string can be converted to “abcba” by inserting “a” and “b”. The number of insertions is two, which is equal to length minus one.
a) 0
b) 1
c) 2
d) 3
View Answer
Clarification: The string can be converted to “efgfe” by inserting “f” or to “egfge” by inserting “g”. Thus, only one insertion is required.
a) 0
b) 1
c) 2
d) 3
View Answer
Clarification: The given string is already a palindrome. So, no insertions are required.
a) Minimum number of jumps problem
b) Longest common subsequence problem
c) Coin change problem
d) Knapsack problems
View Answer
Clarification: A variation of longest common subsequence can be used to solve the minimum number of insertions to form a palindrome problem.#include
a) arr[len][len]
b) len + arr[len][len]
c) len
d) len – arr[len][len]
Clarification: arr[len][len] contains the length of the longest palindromic subsequence. So, len – arr[len][len] gives the minimum number of insertions required to form a palindrome.#include
b) O(n)
c) O(n2)
d) O(mn)
Clarification: The time complexity of the above dynamic programming implementation is O(n2).#include
b) O(n)
c) O(n2)
d) O(mn)
Clarification: The space complexity of the above dynamic programming implementation is O(n2).#include
b) 2
c) 3
d) 4
Clarification: The length of the longest palindromic subsequence is 3. So, the output will be 5 – 3 = 2.#include
b) 3
c) 4
d) 5
Clarification: The value stored in arr[2][4] when the above code is executed is 2.#include
b) 2
c) 4
d) 6
Clarification: Since the string “efgfe” is already a palindrome, the number of insertions required is 0.