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]
  1. A. 3
  2. B. 4
  3. C. 5
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic