Data Structures & AlgorithmsHardICPC regional · algorithm theory

Problem: What is the expected time of Rabin-Karp string matching with a good polynomial hash?

  1. A. O(nm) always
  2. B. O(n + m) expected
  3. C. O(n log m) always
  4. D. O(m^2) always

Correct answer: B. O(n + m) expected

Rolling hashes give O(1) updates per shift and verify candidate matches, so the expected total is linear.

More Coding Interview Problems questions

Exams that ask this topic