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
How does Rabin Karp handle hash collisions?
When hash values match, Rabin Karp checks the actual characters of the substring and pattern to confirm a real match, avoiding false positives from collisions.
Click to reveal answer
intermediate
What is the rolling hash technique in Rabin Karp?
Rolling hash efficiently computes the hash of the next substring by removing the leading character and adding the trailing character, avoiding full recomputation.
Click to reveal answer
intermediate
Why is a prime number often used as the modulus in Rabin Karp hashing?
Using a prime modulus reduces the chance of hash collisions and helps distribute hash values more uniformly.
Click to reveal answer
intermediate
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 causing many character comparisons.
Click to reveal answer
What does Rabin Karp use to quickly compare the pattern with substrings of the text?
ADirect character by character comparison
BHash values
CSorting the text
DBinary search
✗ Incorrect
Rabin Karp uses hash values to quickly compare the pattern with substrings, reducing direct comparisons.
What happens if two substrings have the same hash value in Rabin Karp?
AThey are always the same substring
BThe algorithm ignores the substring
CThe algorithm checks characters to confirm a match
DThe algorithm restarts
✗ Incorrect
Hash collisions can occur, so Rabin Karp verifies by checking characters when hash values match.
What is the purpose of the rolling hash in Rabin Karp?
ATo efficiently update the hash when moving to the next substring
BTo compute hash from scratch each time
CTo sort the pattern
DTo reverse the text
✗ Incorrect
Rolling hash updates the hash value efficiently by removing the old character and adding the new one.
Why is a prime number used as modulus in Rabin Karp hashing?
ATo reduce hash collisions
BTo make the hash smaller
CTo increase collisions
DTo sort the text
✗ Incorrect
Prime modulus helps distribute hash values evenly and reduces collisions.
What is the worst-case time complexity of Rabin Karp?
AO(n + m)
BO(n log m)
CO(m^2)
DO(n * m)
✗ Incorrect
Worst case is O(n*m) due to many hash collisions causing repeated character checks.
Explain how Rabin Karp uses hashing and rolling hash to find a pattern in a text.
Think about how hash values help avoid checking every character every time.
You got /4 concepts.
Describe the advantages and disadvantages of Rabin Karp compared to simple brute force string matching.
Consider speed and when it might slow down.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Rabin Karp String Matching algorithm?
easy
A. Compare each character one by one without hashing
B. Use hashing to find pattern matches efficiently
C. Sort the text and pattern before matching
D. Use recursion to find all pattern occurrences
Solution
Step 1: Understand the algorithm's approach
Rabin Karp uses hashing to convert strings into numbers for quick comparison.
Step 2: Compare hash values instead of characters
It compares hash values of the pattern and text substrings to find matches efficiently.
Final Answer:
Use hashing to find pattern matches efficiently -> Option B
Quick Check:
Hashing = Efficient matching [OK]
Hint: Remember Rabin Karp uses hashing for fast pattern search [OK]
Common Mistakes:
Thinking it compares characters directly always
Confusing with brute force matching
Assuming sorting is involved
2. Which of the following is the correct way to update the rolling hash in Rabin Karp when moving from substring s[i..i+m-1] to s[i+1..i+m]?
easy
A. hash = (hash * base) + s[i] - s[i+m]
B. hash = (hash + s[i] * power) * base - s[i+m]
C. hash = (hash - s[i] * power) * base + s[i+m]
D. hash = (hash - s[i+m] * power) + s[i]
Solution
Step 1: Understand rolling hash update formula
Remove the leftmost character's weighted value, multiply by base, add new character.
Step 2: Apply formula to substring shift
hash = (hash - s[i] * power) * base + s[i+m] correctly updates the hash.
Final Answer:
hash = (hash - s[i] * power) * base + s[i+m] -> Option C
Quick Check:
Remove left char, multiply, add right char [OK]
Hint: Subtract old char, multiply hash, add new char [OK]
Common Mistakes:
Adding instead of subtracting old char
Wrong order of operations
Mixing indices of characters
3. What will be the output of the following C code snippet using Rabin Karp to find pattern "abc" in text "ababc"?
#include
#include
#define base 256
#define prime 101
int main() {
char text[] = "ababc";
char pattern[] = "abc";
int n = strlen(text), m = strlen(pattern);
int p = 0, t = 0, h = 1, i, j;
for (i = 0; i < m-1; i++)
h = (h * base) % prime;
for (i = 0; i < m; i++) {
p = (base * p + pattern[i]) % prime;
t = (base * t + text[i]) % prime;
}
for (i = 0; i <= n - m; i++) {
if (p == t) {
for (j = 0; j < m; j++) {
if (text[i+j] != pattern[j])
break;
}
if (j == m)
printf("Pattern found at index %d\n", i);
}
if (i < n - m) {
t = (base * (t - text[i] * h) + text[i + m]) % prime;
if (t < 0) t = t + prime;
}
}
return 0;
}
medium
A. Pattern found at index 2
B. Pattern found at index 0
C. Pattern found at index 1
D. No pattern found
Solution
Step 1: Calculate initial hashes and check first substring
Pattern "abc" hash matches substring "aba" at index 0? No, characters differ.
Step 2: Slide window and check next substring
At index 1, substring "bab" hash differs. At index 2, substring "abc" matches pattern exactly.
Final Answer:
Pattern found at index 2 -> Option A
Quick Check:
Match at index 2 = Pattern found at index 2 [OK]
Hint: Check hash then verify substring match [OK]
Common Mistakes:
Assuming match at index 0 without verification
Ignoring negative hash correction
Not verifying characters after hash match
4. Identify the bug in this Rabin Karp hash update snippet:
t = (base * (t - text[i] * h) + text[i + m]) % prime;
if (t < 0) t = t - prime;
medium
A. Should add prime instead of subtracting when t < 0
B. Multiplication order is wrong
C. Index i + m is out of range
D. No bug, code is correct
Solution
Step 1: Understand negative hash correction
If hash becomes negative after modulo, add prime to keep it positive.
Step 2: Check given code's correction
Code subtracts prime when t < 0, which makes t more negative. It should add prime.
Final Answer:
Should add prime instead of subtracting when t < 0 -> Option A
Quick Check:
Negative hash fix = add prime [OK]
Hint: Add prime if hash is negative after modulo [OK]
Common Mistakes:
Subtracting prime instead of adding
Ignoring negative hash values
Confusing modulo operation behavior
5. You want to find all occurrences of pattern "aaa" in text "aaaaaa" using Rabin Karp. How many matches will the algorithm report and why?
hard
A. 0 matches, because pattern length equals text length
B. 3 matches, because only non-overlapping occurrences count
C. 1 match, because repeated characters cause hash collision
D. 4 matches, because overlapping occurrences are counted
Solution
Step 1: Count possible substrings of length 3 in text
Text length 6, pattern length 3, so 6 - 3 + 1 = 4 substrings.
Step 2: Check each substring for pattern match
All substrings "aaa" at indices 0,1,2,3 match pattern including overlaps.
Final Answer:
4 matches, because overlapping occurrences are counted -> Option D
Quick Check:
Overlapping matches counted = 4 [OK]
Hint: Count all substrings including overlaps [OK]
Common Mistakes:
Ignoring overlapping matches
Assuming only non-overlapping matches count
Confusing hash collisions with matches
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.