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 performs a direct character-by-character check to confirm the match, because different strings can have the same hash (collision).
Click to reveal answer
intermediate
What is the rolling hash technique in Rabin Karp?
Rolling hash allows efficient calculation of the hash for the next substring by updating the previous hash, avoiding recomputing from scratch for each substring.
Click to reveal answer
intermediate
Why is a prime number often used as the base or modulus in Rabin Karp hashing?
Using a prime number 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 many hash collisions requiring full checks.
Click to reveal answer
What does Rabin Karp use to quickly compare substrings?
AHash values
BDirect character comparison only
CSorting substrings
DBinary search
✗ Incorrect
Rabin Karp uses hash values to quickly compare substrings before verifying matches.
What happens if two substrings have the same hash in Rabin Karp?
AThey are automatically considered a match
BA character-by-character check is done
CThe algorithm stops
DThe hash is recalculated
✗ Incorrect
Because of possible collisions, Rabin Karp verifies by checking characters when hashes match.
What is the purpose of the rolling hash in Rabin Karp?
ATo sort the text
BTo reverse the text
CTo compute hash values efficiently for substrings
DTo encrypt the pattern
✗ Incorrect
Rolling hash updates the hash value efficiently when moving to the next substring.
Which of these is a common choice for the modulus in Rabin Karp hashing?
AA power of two
BZero
CThe length of the text
DA large prime number
✗ Incorrect
A large prime number is used to reduce collisions and distribute hash values evenly.
What is the worst-case time complexity of Rabin Karp?
AO(n*m)
BO(n + m)
CO(log n)
DO(1)
✗ Incorrect
Worst case occurs when many hash collisions cause repeated character checks, leading to O(n*m).
Explain how Rabin Karp uses hashing to find a pattern in a text.
Think about how the algorithm avoids checking every character every time.
You got /4 concepts.
Describe the role of the rolling hash and why it is important in Rabin Karp.
Consider how the hash changes when moving from one substring to the next.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind the Rabin Karp String Matching algorithm?
easy
A. Use hashing to find pattern matches efficiently
B. Use recursion to match patterns
C. Sort the text and pattern before matching
D. Use brute force to check every substring
Solution
Step 1: Understand the algorithm's approach
The Rabin Karp algorithm uses a hash function to convert strings into numbers for quick comparison.
Step 2: Identify the benefit of hashing
Hashing allows checking if substrings match the pattern by comparing hash values instead of characters directly.
Final Answer:
Use hashing to find pattern matches efficiently -> Option A
Quick Check:
Rabin Karp = hashing for matching [OK]
Hint: Remember: Rabin Karp uses hashing to speed up matching [OK]
Common Mistakes:
Thinking it uses recursion
Assuming it sorts strings before matching
Confusing it with brute force checking
2. Which of the following is the correct way to compute the initial hash of a string s of length m using a base d and a prime modulus q in TypeScript?
easy
A. let hash = 0; for(let i=0; i
B. let hash = 1; for(let i=0; i
C. let hash = 0; for(let i=0; i
D. let hash = 0; for(let i=0; i
Solution
Step 1: Understand the rolling hash formula
The hash is computed as hash = (hash * d + current_char) mod q for each character.
Step 2: Match the code with the formula
let hash = 0; for(let i=0; i
Final Answer:
let hash = 0; for(let i=0; i -> Option A
Quick Check:
Rolling hash = (prev_hash * d + char) % q [OK]
Hint: Hash = (prev_hash * base + char) mod prime [OK]
Common Mistakes:
Mixing up base and modulus in calculation
Starting hash from 1 instead of 0
Incorrect order of operations in formula
3. What will be the output of the following TypeScript code snippet using Rabin Karp to find pattern "abc" in text "ababcabc"?
const text = "ababcabc";
const pattern = "abc";
const d = 256;
const q = 101;
function rabinKarp(text: string, pattern: string): number[] {
const m = pattern.length;
const n = text.length;
let p = 0, t = 0, h = 1;
const result: number[] = [];
for (let i = 0; i < m - 1; i++) h = (h * d) % q;
for (let i = 0; i < m; i++) {
p = (d * p + pattern.charCodeAt(i)) % q;
t = (d * t + text.charCodeAt(i)) % q;
}
for (let i = 0; i <= n - m; i++) {
if (p === t) {
let match = true;
for (let j = 0; j < m; j++) {
if (text[i + j] !== pattern[j]) {
match = false;
break;
}
}
if (match) result.push(i);
}
if (i < n - m) {
t = (d * (t - text.charCodeAt(i) * h) + text.charCodeAt(i + m)) % q;
if (t < 0) t += q;
}
}
return result;
}
console.log(rabinKarp(text, pattern));
medium
A. [2, 4, 6]
B. [0, 3, 5]
C. [1, 4]
D. [2, 5]
Solution
Step 1: Identify pattern occurrences in text
The pattern "abc" appears starting at index 2 ("ababcabc") and index 5.
Step 2: Verify code logic for matching indices
The code correctly finds these two positions and returns [2, 5].
Final Answer:
[2, 5] -> Option D
Quick Check:
Pattern found at indices 2 and 5 [OK]
Hint: Check substring start indices where pattern matches [OK]
Common Mistakes:
Including index 0 which is partial match
Confusing overlapping matches
Forgetting to adjust negative hash values
4. The following Rabin Karp implementation has a bug causing incorrect matches. What is the error?
function rabinKarpBug(text: string, pattern: string): number[] {
const m = pattern.length;
const n = text.length;
const d = 256;
const q = 101;
let p = 0, t = 0, h = 1;
const result: number[] = [];
for (let i = 0; i < m; i++) h = (h * d) % q;
for (let i = 0; i < m; i++) {
p = (d * p + pattern.charCodeAt(i)) % q;
t = (d * t + text.charCodeAt(i)) % q;
}
for (let i = 0; i <= n - m; i++) {
if (p === t) {
let match = true;
for (let j = 0; j < m; j++) {
if (text[i + j] !== pattern[j]) {
match = false;
break;
}
}
if (match) result.push(i);
}
if (i < n - m) {
t = (d * (t - text.charCodeAt(i) * h) + text.charCodeAt(i + m)) % q;
if (t < 0) t += q;
}
}
return result;
}
medium
A. The hash update formula inside the loop is missing modulo operation
B. The initial value of h is computed incorrectly; it should be for (i = 0; i < m-1; i++)
C. The pattern hash p is not computed correctly
D. The loop for checking matches uses wrong indices
Solution
Step 1: Check computation of h
Variable h should be (d^(m-1)) % q, but loop runs m times, making h too large.
Step 2: Understand impact on rolling hash
Incorrect h causes wrong subtraction in rolling hash, leading to incorrect matches.
Final Answer:
The initial value of h is computed incorrectly; it should be for (i = 0; i < m-1; i++) -> Option B
Quick Check:
h loop runs m-1 times, not m [OK]
Hint: Compute h with loop to m-1, not m [OK]
Common Mistakes:
Calculating h with wrong loop range
Forgetting modulo in hash updates
Misindexing pattern or text in loops
5. You want to use Rabin Karp to find all occurrences of pattern "aaa" in text "aaaaaa". What is the output of the algorithm and why is it important to verify matches after hash comparison?
hard
A. [0, 1, 2, 3, 4], because every substring is checked without hash
B. [0, 3], because only non-overlapping matches are counted
C. [0, 1, 2, 3], because overlapping matches exist and hash collisions can occur
D. [0], because Rabin Karp only finds first match
Solution
Step 1: Identify all pattern occurrences in text
The pattern "aaa" appears starting at indices 0, 1, 2, and 3 in "aaaaaa" due to overlapping.
Step 2: Explain importance of verifying matches after hash
Hash collisions can cause false positives, so character-by-character check confirms actual matches.
Final Answer:
[0, 1, 2, 3], because overlapping matches exist and hash collisions can occur -> Option C
Quick Check:
Overlapping matches + verify after hash = [0,1,2,3] [OK]
Hint: Check overlapping matches and confirm after hashing [OK]
Common Mistakes:
Ignoring overlapping matches
Assuming hash guarantees no false positives
Counting only first or non-overlapping matches
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.