Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Which statement about the KMP failure (lps) array of a pattern of length m is correct?

  1. A. It depends on the text being searched
  2. B. lps[i] can equal i + 1
  3. C. It can be built in O(m), and lps[i] ≤ i for every index i
  4. D. Building it takes O(m²) in the worst case

Correct answer: C. It can be built in O(m), and lps[i] ≤ i for every index i

Amortised analysis (k increases at most m times overall) gives O(m). lps[i] is a PROPER prefix length of p[0..i], so it is at most i. It depends only on the pattern.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic