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)- A. 1 1
- B. 0 1
- C. 3 1
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "beeebdddcbadad" with all characters…
- What is the length of the smallest substring of "bcdadddaacbac" that contains every distinct character of the string at least once?…
- Which lower bound holds for every comparison-based sorting algorithm in the worst case?
- What does this loop-counting code print? (Python) python n, c = 12, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)
- Product of all elements except self, using prefix and suffix products (no division). What is printed? python a = [1, 1, 0, 2, -1] n =…
- Every row and every column of this 4 × 4 matrix is sorted ascending (rows separated by ';'): 9 11 16 36; 14 19 37 42; 29 40 45 50; 41 46…