Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

For the pattern "aabaabaaab" (0-based), what is the value of the KMP prefix function π at the LAST index? π[i] is the length of the longest proper prefix of p[0..i] that is also a suffix of it.

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

Correct answer: A. 3

Compute π with the standard failure loop: on a mismatch fall back through π[k−1] before comparing again. Reporting the value one index early, or the complement length, or the number of nonzero entries are the typical slips.

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

Exams that ask this topic