Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is printed?

Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is printed?
t, p, q = "25205698121476", "81", 7
m = len(p); hp = int(p) % q
true = spur = 0
for i in range(len(t) - m + 1):
    if int(t[i:i + m]) % q == hp:
        if t[i:i + m] == p: true += 1
        else: spur += 1
print(true, spur)
  1. A. 1 1
  2. B. 0 1
  3. C. 3 1
  4. D. 1 2

Correct answer: A. 1 1

Every window whose value ≡ pattern (mod q) is a hit; only those equal character-by-character are true matches. Output: 1 1.

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

Exams that ask this topic