ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (strings are indexed from 0)
What will be the output of the following pseudocode? (strings are indexed from 0)
String s = "AEBCBDA"
String t = reverse of s
Integer n = 7, L[0..7][0..7] = all 0
for (each i from 1 to n)
for (each j from 1 to n)
if (s[i - 1] == t[j - 1])
L[i][j] = L[i - 1][j - 1] + 1
else
L[i][j] = max(L[i - 1][j], L[i][j - 1])
end if
end for
end for
Print L[n][n], n - L[n][n]- A. 3 4
- B. 5 2
- C. 7 0
- D. 4 3
Correct answer: B. 5 2
The LCS of a string and its reverse is its longest palindromic subsequence, here ABCBA (length 5). The other 2 letters (E and D) must be deleted to leave a palindrome: 5 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer p[6] = {7, 1, 5, 3, 6, 4} Integer lo = p[0], one = 0, many = 0 for (each i…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {2, 3, 1, 1, 4} Integer jumps = 0, curEnd…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer h[9] = {1, 8, 6, 2, 5, 4, 8, 3, 7} Integer i = 0…
- What will be the output of the following pseudocode? (>> is right shift, & is AND) Integer a[3] = {4, 14, 2}, total = 0 for (each b from 0…
- What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts) Integer x = 1, r = 0 for (each i from 1 to…
- What will be the output of the following pseudocode? Integer m[3][3] = {{2, 0, 1}, {1, 3, 2}, {1, 1, 1}} Integer det = m[0][0] (m[1][1]…