ProgrammingVery hardTCS Digital · Infosys InfyTQ

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String A = "AXYT", B = "AYZX"
Integer L[5][5] = all 0, i, j
for (i = 1; i <= 4; i = i + 1)
    for (j = 1; j <= 4; j = j + 1)
        if (A[i-1] == B[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[4][4]
  1. A. 3
  2. B. 1
  3. C. 4
  4. D. 2

Correct answer: D. 2

This is the longest common subsequence length. The best common subsequences are AY and AX, each of length 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic