Data Structures & AlgorithmsHardGATE CS · DSA

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 = "83263649654834", "49", 11
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. 0 0
  2. B. 1 1
  3. C. 3 0
  4. D. 1 0

Correct answer: D. 1 0

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

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

Exams that ask this topic