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)- A. 0 0
- B. 1 1
- C. 3 0
- 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.
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
- A fixed window of length 3 slides over s = "baacbbbcbcabab" with a character-count array. At how many starting positions is the window an…
- Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and quadratic probing (h(k) + i²) mod 10. Which statement is correct?
- What does this loop-counting code print? (Python) python n, c = 64, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 4 8 2 1; 4 3 1 4; 7 6 2 4…
- Which sorting algorithms are stable in their standard array implementations: insertion sort, selection sort, merge sort, heap sort, quick…
- For each element of [23, 12, 7, 21, 25, 8, 13, 1], find the next greater element to its RIGHT (−1 if none), using a monotonic stack. What…