What is the main idea behind the Rabin Karp string matching algorithm?
It uses hashing to find a pattern in a text by comparing hash values of the pattern and substrings of the text, reducing the number of direct character comparisons.
Click to reveal answer
intermediate
Why does Rabin Karp use a rolling hash?
A rolling hash allows efficient computation of hash values for consecutive substrings by updating the previous hash instead of recalculating from scratch.
Click to reveal answer
intermediate
What happens if two substrings have the same hash value in Rabin Karp?
Since hash collisions can occur, the algorithm checks the actual characters of the substrings to confirm if they really match.
Click to reveal answer
advanced
What is the average and worst-case time complexity of Rabin Karp?
Average case is O(n + m) where n is text length and m is pattern length. Worst case is O(n*m) due to hash collisions requiring full substring checks.
Click to reveal answer
intermediate
Explain the role of the modulus in Rabin Karp hashing.
The modulus keeps hash values within a fixed range to avoid overflow and helps in distributing hash values uniformly to reduce collisions.
Click to reveal answer
What does Rabin Karp primarily use to speed up pattern matching?
AHashing of substrings
BSorting the text
CBinary search
DDynamic programming
✗ Incorrect
Rabin Karp uses hashing to quickly compare the pattern with substrings of the text.
What is a rolling hash in Rabin Karp?
AA hash that sorts characters
BA hash that is recalculated fully each time
CA hash that ignores collisions
DA hash that updates efficiently for the next substring
✗ Incorrect
Rolling hash updates the hash value by removing the old character and adding the new one efficiently.
If two substrings have the same hash, what does Rabin Karp do next?
AAssumes they match without checking
BIgnores the substrings
CChecks characters one by one to confirm match
DRestarts the algorithm
✗ Incorrect
Because of possible collisions, Rabin Karp verifies the actual substring characters.
What is the worst-case time complexity of Rabin Karp?
AO(log n)
BO(n*m)
CO(m^2)
DO(n + m)
✗ Incorrect
Worst case happens when many hash collisions cause repeated substring checks, leading to O(n*m).
Why is modulus used in Rabin Karp hashing?
ATo keep hash values in a fixed range and reduce collisions
BTo sort the hash values
CTo increase hash values indefinitely
DTo ignore characters in the pattern
✗ Incorrect
Modulus keeps hash values manageable and helps distribute them evenly.
Describe how the Rabin Karp algorithm finds a pattern in a text using hashing.
Think about how hash values help avoid checking every character every time.
You got /4 concepts.
Explain why Rabin Karp might sometimes perform slower than expected and how it handles that situation.
Consider what happens when different substrings have the same hash.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind the Rabin Karp String Matching algorithm?
easy
A. It uses hashing to find pattern matches efficiently.
B. It compares each character one by one without hashing.
C. It sorts the text before searching for the pattern.
D. It uses recursion to find the pattern in the text.
Solution
Step 1: Understand Rabin Karp's approach
The algorithm uses a hash function to convert the pattern and text substrings into numbers.
Step 2: Use hashing for efficient matching
By comparing hash values instead of characters, it quickly finds potential matches.
Final Answer:
It uses hashing to find pattern matches efficiently. -> Option A
Quick Check:
Hashing = Efficient matching [OK]
Hint: Remember: Rabin Karp uses hashing for fast pattern search [OK]
Common Mistakes:
Thinking it compares characters directly without hashing
Confusing it with sorting-based algorithms
Assuming it uses recursion
2. Which of the following is the correct way to compute the rolling hash in Rabin Karp for the next substring?
easy
A. new_hash = old_hash * old_char + new_char
B. new_hash = old_hash + old_char - new_char
C. new_hash = (old_hash - old_char * base^(m-1)) * base + new_char
D. new_hash = old_hash / base + new_char * base^(m-1)
Solution
Step 1: Recall rolling hash formula
Rolling hash removes the old char's contribution and adds the new char's value.
Step 2: Apply formula correctly
It subtracts old_char * base^(m-1), multiplies by base, then adds new_char.
Final Answer:
new_hash = (old_hash - old_char * base^(m-1)) * base + new_char -> Option C
Quick Check:
Rolling hash formula = new_hash = (old_hash - old_char * base^(m-1)) * base + new_char [OK]
Hint: Subtract old char, multiply by base, add new char [OK]
Common Mistakes:
Adding old_char instead of subtracting
Dividing instead of multiplying by base
Ignoring power of base for old_char
3. What will be the output of the following code snippet using Rabin Karp to find pattern 'abc' in text 'ababc'?
def rabin_karp(text, pattern):
base = 256
prime = 101
m = len(pattern)
n = len(text)
pattern_hash = 0
text_hash = 0
h = 1
for i in range(m-1):
h = (h * base) % prime
for i in range(m):
pattern_hash = (base * pattern_hash + ord(pattern[i])) % prime
text_hash = (base * text_hash + ord(text[i])) % prime
for i in range(n - m + 1):
if pattern_hash == text_hash:
if text[i:i+m] == pattern:
return i
if i < n - m:
text_hash = (base * (text_hash - ord(text[i]) * h) + ord(text[i + m])) % prime
if text_hash < 0:
text_hash += prime
return -1
print(rabin_karp('ababc', 'abc'))
medium
A. 0
B. 1
C. -1
D. 2
Solution
Step 1: Calculate pattern and initial text hash
Pattern 'abc' length 3, initial text substring 'aba'. Hashes differ, so move forward.
Step 2: Slide over text and compare hashes
At index 1, substring 'bab' hash differs. At index 2, substring 'abc' hash matches pattern hash and strings match.
Final Answer:
2 -> Option D
Quick Check:
Pattern found at index 2 [OK]
Hint: Check hash then substring; match at index 2 [OK]
Common Mistakes:
Returning first hash match without substring check
Confusing index positions
Ignoring negative hash adjustment
4. Identify the bug in this Rabin Karp implementation snippet:
text_hash = (base * (text_hash - ord(text[i]) * h) + ord(text[i + m])) % prime
if text_hash < 0:
text_hash = text_hash - prime
medium
A. The negative hash correction should add prime, not subtract.
B. The subtraction of ord(text[i]) * h is incorrect.
C. The modulo operation should be done after adding prime.
D. The base should be added, not multiplied.
Solution
Step 1: Understand negative hash correction
If hash is negative after modulo, add prime to make it positive.
Step 2: Identify incorrect operation
Code subtracts prime instead of adding it, causing wrong hash values.
Final Answer:
The negative hash correction should add prime, not subtract. -> Option A
Quick Check:
Negative hash fix = add prime [OK]
Hint: Add prime if hash is negative, never subtract [OK]
Common Mistakes:
Subtracting prime instead of adding
Ignoring negative hash values
Misplacing modulo operation
5. Given a large text and multiple patterns, which modification to Rabin Karp helps efficiently find all patterns in the text?
hard
A. Run Rabin Karp separately for each pattern without optimization.
B. Use a combined hash set of all patterns and check text substrings against it.
C. Sort all patterns and use binary search on text substrings.
D. Use recursion to search each pattern in the text.