Data Structures & AlgorithmsHardProduct Company SDE · Infosys OA

Problem: What is the length of the longest common substring (contiguous) of "abcdxyz" and "xyzabcd"?

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

Correct answer: D. 4

Check the contiguous match starting at each pair of positions and keep the longest. Taking the longest common subsequence instead gives a larger value, since the subsequence need not be contiguous.

More Coding Interview Problems questions

Exams that ask this topic