Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

Using z[0] = 0, what is the sum of the Z-function values over all indices of "aabxaayaab"?

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

Correct answer: C. 8

The Z-value at i is the longest common prefix of s and s[i:]. Summing these with z[0] = 0 gives the answer, and a naive recalculation matches the linear-time algorithm.

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

Exams that ask this topic