Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Problem: Compute the KMP prefix function (the longest proper prefix that is also a suffix, for each prefix) of the pattern "abacabab". What is the value at the last position (index 7)?

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

Correct answer: B. 2

The last prefix 'abacabab' has borders 'ab' (length 2) as its longest proper prefix-suffix match, so pi[7] = 2. Taking the single letter 'b' or the length of 'aba' would be the common slip.

More Coding Interview Problems questions

Exams that ask this topic