Data Structures & AlgorithmsHardICPC regional · string round

Problem: Which claim about the Knuth-Morris-Pratt algorithm is correct?

  1. A. It is O(n * m) in the worst case because of the fallback loop
  2. B. It needs O(n^2) preprocessing of the pattern
  3. C. It only works when the pattern has no repeated characters
  4. D. Its total work is O(n + m) because the pattern pointer never moves back more than it has moved forward

Correct answer: D. Its total work is O(n + m) because the pattern pointer never moves back more than it has moved forward

Each text character advances the text pointer once, and each failure only falls back along the prefix function, which the amortised argument bounds by the total advances. The fallback is not a per-character quadratic step.

More Coding Interview Problems questions

Exams that ask this topic