Skip to content
Mad4Study

Engineering & Science

GATE Computer Science

  • GATE CS
  • 2026–27 pattern practice
  • Algorithms
  • Dynamic programming
  • Hard

What is the length of the longest common subsequence of X = ABCBDAB and Y = BDCABA?

Algorithm Design

Pick an option to check your answer.

Filling the standard LCS table, L[i][j]=L[i−1][j−1]+1L[i][j] = L[i-1][j-1] + 1 when the characters match, else max⁡(L[i−1][j], L[i][j−1])\max(L[i-1][j],\, L[i][j-1]), gives L[7][6]=4L[7][6] = 4. One LCS is BCBA; others are BDAB and BCAB.

Why not the others

  • 3 misses one match (for example taking only BCA).
  • 5 and 6 — no common subsequence that long exists; Y has only 6 characters and its order cannot be matched fully in X.

Report an error

GATE CS · Algorithms · Algorithm Design