This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Longest Palindromic Subsequence”.

1. Which of the following methods can be used to solve the longest palindromic subsequence problem?

a) Dynamic programming

b) Recursion

c) Brute force

d) All of the mentioned

View Answer

Explanation: All of the mentioned methods can be used to solve the longest palindromic subsequence problem.

2. Which of the following strings is a palindromic subsequence of the string “ababcdabba”?

a) abcba

b) abba

c) abbbba

d) all of the mentioned

View Answer

Explanation: All of the mentioned strings are palindromic subsequences of the string “ababcdabba”.

3. For which of the following, the length of the string is equal to the length of the longest palindromic subsequence?

a) A string that is a palindrome

b) A string of length one

c) A string that has all the same letters(e.g. aaaaaa)

d) All of the mentioned

View Answer

Explanation: In all the above cases, the string itself is the longest palindromic subsequence and hence the length of the longest palindromic subsequence is equal to the length of the string.

4. What is the length of the longest palindromic subsequence for the string “ababcdabba”?

a) 6

b) 7

c) 8

d) 9

View Answer

Explanation: The longest palindromic subsequence is “abbabba” and its length is 7.

5. What is the time complexity of the brute force algorithm used to find the length of the longest palindromic subsequence?

a) O(1)

b) O(2^{n})

c) O(n)

d) O(n^{2})

View Answer

Explanation: In the brute force algorithm, all the subsequences are found and the length of the longest palindromic subsequence is calculated. This takes exponential time.

6. For every non-empty string, the length of the longest palindromic subsequence is at least one.

a) True

b) False

View Answer

Explanation: A single character of any string can always be considered as a palindrome and its length is one.

7. Longest palindromic subsequence is an example of ______________

a) Greedy algorithm

b) 2D dynamic programming

c) 1D dynamic programming

d) Divide and conquer

View Answer

Explanation: Longest palindromic subsequence is an example of 2D dynamic programming.

8. Consider the following code:

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lps(char *str1) { int i,j,len; len = strlen(str1); char str2[len + 1]; strcpy(str2, str1); ______________; int arr[len + 1][len + 1]; for(i = 0; i <= len; i++) arr[i][0] = 0; for(i = 0; i <= len; i++) arr[0][i] = 0; for(i = 1; i <= len; i++) { for(j = 1; j <= len; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len][len]; } int main() { char str1[] = "ababcdabba"; int ans = lps(str1); printf("%d",ans); return 0; }

Which of the following lines completes the above code?

a) strrev(str2)

b) str2 = str1

c) len2 = strlen(str2)

d) none of the mentioned

View Answer

Explanation: To find the longest palindromic subsequence, we need to reverse the copy of the string, which is done by strrev.

9. What is the time complexity of the above dynamic programming implementation to find the longest palindromic subsequence where the length of the string is n?

a) O(n)

b) O(1)

c) O(n^{2})

d) None of the mentioned

View Answer

Explanation: The time complexity of the above dynamic programming implementation to find the longest palindromic subsequence is O(n

^{2}).

10. What is the space complexity of the above dynamic programming implementation to find the longest palindromic subsequence where the length of the string is n?

a) O(n)

b) O(1)

c) O(n^{2})

d) None of the mentioned

View Answer

Explanation: The space complexity of the above dynamic programming implementation to find the longest palindromic subsequence is O(n

^{2}).

11. What is the value stored in arr[3][3] when the following code is executed?

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lps(char *str1) { int i,j,len; len = strlen(str1); char str2[len + 1]; strcpy(str2, str1); strrev(str2); int arr[len + 1][len + 1]; for(i = 0; i <= len; i++) arr[i][0] = 0; for(i = 0; i <= len; i++) arr[0][i] = 0; for(i = 1; i <= len; i++) { for(j = 1; j <= len; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len][len]; } int main() { char str1[] = "ababcdabba"; int ans = lps(str1); printf("%d",ans); return 0; }

a) 2

b) 3

c) 4

d) 5

View Answer

Explanation: The value stored in arr[3][3] when the above code is executed is 2.

12. What is the output of the following code?

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lps(char *str1) { int i,j,len; len = strlen(str1); char str2[len + 1]; strcpy(str2, str1); strrev(str2); int arr[len + 1][len + 1]; for(i = 0; i <= len; i++) arr[i][0] = 0; for(i = 0; i <= len; i++) arr[0][i] = 0; for(i = 1; i <= len; i++) { for(j = 1; j <= len; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len][len]; } int main() { char str1[] = "abcd"; int ans = lps(str1); printf("%d",ans); return 0; }

a) 0

b) 1

c) 2

d) None of the mentioned

View Answer

Explanation: The program prints the length of the longest palindromic subsequence, which is 1.

13. What is the output of the following code?

#include<stdio.h> #include<string.h> int max_num(int a, int b) { if(a > b) return a; return b; } int lps(char *str1) { int i,j,len; len = strlen(str1); char str2[len + 1]; strcpy(str2, str1); strrev(str2); int arr[len + 1][len + 1]; for(i = 0; i <= len; i++) arr[i][0] = 0; for(i = 0; i <= len; i++) arr[0][i] = 0; for(i = 1; i <= len; i++) { for(j = 1; j <= len; j++) { if(str1[i-1] == str2[j - 1]) arr[i][j] = 1 + arr[i - 1][j - 1]; else arr[i][j] = max_num(arr[i - 1][j], arr[i][j - 1]); } } return arr[len][len]; } int main() { char str1[] = "abdgkagdjbccbba"; int ans = lps(str1); printf("%d",ans); return 0; }

a) 5

b) 7

c) 9

d) 11

View Answer

Explanation: The program prints the length of the longest palindromic subsequence, which is 9.

**Sanfoundry Global Education & Learning Series – Data Structure.**

To practice all areas of Data Structure, __here is complete set of 1000+ Multiple Choice Questions and Answers__.