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 x = "AGGTAB", y = "GXTXAYB"
Integer L[0..6][0..7] = all 0
for (each i from 1 to 6)
for (each j from 1 to 7)
if (x[i - 1] == y[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[6][7]- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
The table is the longest-common-subsequence DP. A longest common subsequence is GTAB (G, T, A, B appear in this order in both strings), and no 5-letter one exists, so the output is 4.
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 wt[3] = {2, 3, 4}, val[3] = {3, 5, 6} Integer best[0..6] = all 0 for (each i…
- What will be the output of the following pseudocode? Integer lcm = 18, steps = 0 Integer a = 12, b = 18 while (lcm mod a != 0 OR lcm mod b…
- What will be the output of the following pseudocode? Integer total = 0 Procedure p(Integer n) if (n <= 0) return end if total = total + n…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 8) for (each j from i + 1 to 9) for (each k from…
- What will be the output of the following pseudocode? Function f(Integer a, Integer b) return a b - (a + b) End Function Print f(f(2, 3)…
- What will be the output of the following pseudocode? (Strings are indexed from 0) Integer calls = 0 Function pal(String s, Integer i…