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 Java code snippet that calculates the initial hash for the pattern "abc" using a base of 256 and a prime modulus 101?
DSA Java
int base = 256; int prime = 101; String pattern = "abc"; int hash = 0; for (int i = 0; i < pattern.length(); i++) { hash = (hash * base + pattern.charAt(i)) % prime; } System.out.println(hash);
Attempts:
2 left
💡 Hint
Calculate hash step by step using modulo arithmetic.
✗ Incorrect
The hash is calculated as (((0 * 256 + 'a') % 101) * 256 + 'b') % 101 * 256 + 'c') % 101. The final result is 90.
🧠 Conceptual
intermediateWhy use a prime modulus in Rabin Karp?
Why is a prime number used as the modulus in the Rabin Karp algorithm's hash function?
Attempts:
2 left
💡 Hint
Think about how hash collisions affect string matching accuracy.
✗ Incorrect
Using a prime modulus helps distribute hash values more uniformly, reducing collisions and improving the algorithm's efficiency.
🔧 Debug
advancedIdentify the error in rolling hash update
What error will occur when running this Java code snippet that updates the rolling hash in Rabin Karp?
DSA Java
int base = 256; int prime = 101; int oldHash = 94; int highBase = 256 * 256 % prime; // supposed to be base^(patternLength-1) mod prime int oldChar = 'a'; int newChar = 'd'; int newHash = (oldHash - oldChar * highBase) * base + newChar % prime; System.out.println(newHash);
Attempts:
2 left
💡 Hint
Check operator precedence and modulo placement carefully.
✗ Incorrect
The modulo operator applies only to newChar, not the entire expression, causing incorrect hash calculation.
🚀 Application
advancedFind pattern occurrences using Rabin Karp
Given the text "abracadabra" and pattern "abra", which starting indices will the Rabin Karp algorithm find the pattern at?
Attempts:
2 left
💡 Hint
Check all substrings of length equal to pattern and compare hashes.
✗ Incorrect
The pattern "abra" appears at index 0 and index 7 in the text.
❓ Predict Output
expertOutput of Rabin Karp with collision check
What is the output of this Java code that uses Rabin Karp to find all occurrences of "test" in "this is a test text with test cases"?
DSA Java
import java.util.*; public class RabinKarp { public static void main(String[] args) { String text = "this is a test text with test cases"; String pattern = "test"; int base = 256; int prime = 101; int m = pattern.length(); int n = text.length(); int patternHash = 0, textHash = 0, h = 1; List<Integer> result = new ArrayList<>(); for (int i = 0; i < m - 1; i++) h = (h * base) % prime; for (int i = 0; i < m; i++) { patternHash = (base * patternHash + pattern.charAt(i)) % prime; textHash = (base * textHash + text.charAt(i)) % prime; } for (int i = 0; i <= n - m; i++) { if (patternHash == textHash) { int j; for (j = 0; j < m; j++) { if (text.charAt(i + j) != pattern.charAt(j)) break; } if (j == m) result.add(i); } if (i < n - m) { textHash = (base * (textHash - text.charAt(i) * h) + text.charAt(i + m)) % prime; if (textHash < 0) textHash += prime; } } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check all occurrences of "test" in the text carefully.
✗ Incorrect
The pattern "test" appears twice: starting at index 10 and index 25 in the text.
