Data Structures & AlgorithmsHardTCS Digital · Infosys SP
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 = "29199487294502", "29", 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. 2 3
- B. 2 4
- C. 4 2
- D. 2 2
Correct answer: D. 2 2
Every window whose value ≡ pattern (mod q) is a hit; only those equal character-by-character are true matches. Output: 2 2.
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
- 11 people stand in a circle numbered 1 to 11. Counting starts at person 1, and every 4th person is removed (counting continues from the…
- Kadane's algorithm runs on [−4, −7, 9, 3, −5, 4, 1, 4, 8]. What is the maximum sum of a non-empty contiguous subarray?
- How many contiguous subarrays of [3, 2, −3, 5, 2, −1, 0, 1, 2] have sum exactly 3? (Prefix sums with a hash map of prefix-sum frequencies.)
- How many pairs (i < j) of [6, 27, 29, 25, 10, 6, 25, 3, 7, 22] have a[i] + a[j] divisible by 6? (Count remainders mod 6 and pair r with 6…
- A queue is built from two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving every element of IN onto OUT only when OUT…
- LSD radix sort (base 10, stable sort on each digit, units digit first) is applied to [148, 261, 414, 335, 678, 773, 321]. What is the list…