Challenge - 5 Problems
Rabin Karp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Rabin Karp hash calculation
What is the output of the following C++ code that calculates the initial hash values for pattern and text substring using Rabin Karp algorithm?
DSA C++
const int d = 256; const int q = 101; int calculateHash(const std::string &str, int length) { int hash = 0; for (int i = 0; i < length; i++) { hash = (d * hash + str[i]) % q; } return hash; } int main() { std::string pattern = "abc"; std::string text = "abcd"; int pHash = calculateHash(pattern, pattern.size()); int tHash = calculateHash(text, pattern.size()); std::cout << pHash << " " << tHash << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Calculate hash using formula: hash = (d * previous_hash + current_char) % q for each character.
✗ Incorrect
The hash for 'abc' and 'abc' substring in 'abcd' both compute to 90 using the given formula with d=256 and q=101.
🧠 Conceptual
intermediateUnderstanding rolling hash update in Rabin Karp
In Rabin Karp algorithm, when moving the window by one character in the text, how is the rolling hash updated efficiently?
Attempts:
2 left
💡 Hint
Think about how to avoid recomputing the entire hash for each substring.
✗ Incorrect
The rolling hash removes the contribution of the old character, shifts the hash by multiplying with base, adds the new character, and takes modulo to keep hash within range.
🔧 Debug
advancedIdentify the error in Rabin Karp substring search code
What error will the following C++ code produce when searching for pattern "abc" in text "ababc" using Rabin Karp?
DSA C++
const int d = 256; const int q = 101; int main() { std::string text = "ababc"; std::string pattern = "abc"; int n = text.size(); int m = pattern.size(); int pHash = 0, tHash = 0, h = 1; for (int i = 0; i < m - 1; i++) h = (h * d) % q; for (int i = 0; i < m; i++) { pHash = (d * pHash + pattern[i]) % q; tHash = (d * tHash + text[i]) % q; } for (int i = 0; i <= n - m; i++) { if (pHash == tHash) { bool match = true; for (int j = 0; j < m; j++) { if (text[i + j] != pattern[j]) { match = false; break; } } if (match) { std::cout << "Pattern found at index " << i << std::endl; } } if (i < n - m) { tHash = (d * (tHash - text[i] * h) + text[i + m]) % q; if (tHash < 0) tHash += q; } } return 0; }
Attempts:
2 left
💡 Hint
Check the substring matches and hash updates carefully.
✗ Incorrect
The pattern "abc" appears starting at index 2 in "ababc". The code correctly finds it once at index 2.
🚀 Application
advancedNumber of pattern occurrences using Rabin Karp
Given text "aaaaa" and pattern "aa", how many times will the Rabin Karp algorithm find the pattern in the text?
Attempts:
2 left
💡 Hint
Count overlapping occurrences of the pattern in the text.
✗ Incorrect
Pattern "aa" appears starting at indices 0,1,2,3 in "aaaaa", total 4 times including overlaps.
🧠 Conceptual
expertWhy is a prime number used as modulus in Rabin Karp?
Why does the Rabin Karp algorithm use a prime number as the modulus (q) in its hash calculations?
Attempts:
2 left
💡 Hint
Think about how modulus affects hash distribution and collisions.
✗ Incorrect
Using a prime modulus reduces collisions by distributing hash values more uniformly, improving algorithm accuracy.
