Challenge - 5 Problems
Rabin Karp Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Rabin Karp hash calculation
What is the output of the following code snippet that calculates the initial hash values for pattern and text substring?
DSA C
int prime = 101; int d = 256; int pattern_len = 3; char pattern[] = "abc"; char text[] = "abcd"; int pattern_hash = 0, text_hash = 0, h = 1; for (int i = 0; i < pattern_len - 1; i++) h = (h * d) % prime; for (int i = 0; i < pattern_len; i++) { pattern_hash = (d * pattern_hash + pattern[i]) % prime; text_hash = (d * text_hash + text[i]) % prime; } printf("%d %d %d", pattern_hash, text_hash, h);
Attempts:
2 left
💡 Hint
Calculate hash step by step using modulo arithmetic with prime 101.
✗ Incorrect
The hash for 'abc' and 'abc' substring in text both compute to 6 with base 256 and modulo 101. The value h is d^(pattern_len-1) mod prime = 256^2 mod 101 = 16.
❓ Predict Output
intermediate2:00remaining
Rabin Karp rolling hash update output
What is the output of the rolling hash update step in Rabin Karp for the given text and pattern?
DSA C
int prime = 101; int d = 256; int pattern_len = 3; char text[] = "abcd"; int h = 16; // precomputed as 256^(3-1) % 101 int text_hash = 6; // hash of 'abc' // Update hash to next substring 'bcd' text_hash = (d * (text_hash - text[0] * h) + text[3]) % prime; if (text_hash < 0) text_hash += prime; printf("%d", text_hash);
Attempts:
2 left
💡 Hint
Apply the rolling hash formula carefully and adjust for negative values.
✗ Incorrect
The rolling hash removes the contribution of 'a' (text[0]) and adds 'd' (text[3]). The result modulo 101 is 7.
🧠 Conceptual
advanced2:00remaining
Why use a prime number in Rabin Karp hashing?
Why is a prime number used as the modulus in the Rabin Karp hashing algorithm?
Attempts:
2 left
💡 Hint
Think about how prime modulus affects hash distribution.
✗ Incorrect
Using a prime modulus helps spread hash values more evenly, reducing collisions and improving matching accuracy.
🔧 Debug
advanced2:00remaining
Identify the error in this Rabin Karp substring search snippet
What error will this code snippet cause when searching for pattern in text using Rabin Karp?
DSA C
int prime = 101; int d = 256; int pattern_len = 3; char pattern[] = "abc"; char text[] = "abcd"; int pattern_hash = 0, text_hash = 0, h = 1; for (int i = 0; i < pattern_len - 1; i++) h = (h * d) % prime; for (int i = 0; i < pattern_len; i++) { pattern_hash = (d * pattern_hash + pattern[i]) % prime; text_hash = (d * text_hash + text[i]) % prime; } for (int i = 0; i <= strlen(text) - pattern_len; i++) { if (pattern_hash == text_hash) { for (int j = 0; j < pattern_len; j++) { if (text[i + j] != pattern[j]) break; if (j == pattern_len - 1) printf("Pattern found at index %d", i); } } if (i < strlen(text) - pattern_len) { text_hash = (d * (text_hash - text[i] * h) + text[i + pattern_len]) % prime; if (text_hash < 0) text_hash += prime; } }
Attempts:
2 left
💡 Hint
Check when the print statement executes inside the inner loop.
✗ Incorrect
The print statement is inside the inner loop and can execute multiple times if characters match partially, causing repeated prints for the same index.
🚀 Application
expert2:00remaining
Number of hash comparisons in worst case for Rabin Karp
Given a text of length N and a pattern of length M, what is the worst-case number of hash comparisons Rabin Karp algorithm performs?
Attempts:
2 left
💡 Hint
Consider the worst case when many hash collisions occur.
✗ Incorrect
In worst case, many hash collisions cause character-by-character checks for each substring, leading to O(M * (N - M + 1)) comparisons.
