Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

What is the length of the longest common subsequence of PLACEMENT and COMPLETE?

  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: C. 4

The DP table (L[i][j] = L[i−1][j−1] + 1 on a match, else the max of the two neighbours) ends at 4. One LCS is C, M, E, T (PLA-C-E-M-E-N-T vs C-O-M-P-L-E-T-E: C, M, E, T appear in order in both); another is P, L, E, T. Five would need, e.g., P L E T E, but PLACEMENT has no E after its T.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic