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 Rust code that calculates the initial hash for the pattern and the first window of the text using Rabin Karp algorithm?
DSA Rust
fn main() {
let text = "abcde";
let pattern = "abc";
let base: u64 = 256;
let prime: u64 = 101;
let m = pattern.len();
let mut pattern_hash = 0u64;
let mut text_hash = 0u64;
let mut h = 1u64;
for _ in 0..m-1 {
h = (h * base) % prime;
}
for i in 0..m {
pattern_hash = (base * pattern_hash + pattern.as_bytes()[i] as u64) % prime;
text_hash = (base * text_hash + text.as_bytes()[i] as u64) % prime;
}
println!("pattern_hash: {}", pattern_hash);
println!("text_hash: {}", text_hash);
}Attempts:
2 left
💡 Hint
Remember to apply modulo operation after each multiplication and addition.
✗ Incorrect
The code calculates the hash of the pattern "abc" and the first window "abc" in the text using base 256 and prime 101. The resulting hash values are both 6.
🧠 Conceptual
intermediateUnderstanding the rolling hash update in Rabin Karp
In Rabin Karp algorithm, when sliding the window by one character, how is the new hash value computed from the old hash value?
Attempts:
2 left
💡 Hint
Think about how to efficiently update the hash without full recomputation.
✗ Incorrect
The rolling hash removes the contribution of the leftmost character, shifts the hash by multiplying with base, adds the new character, and applies modulo to keep the hash within range.
🔧 Debug
advancedIdentify the error in this Rabin Karp hash update code
What error will this Rust code produce when updating the rolling hash in Rabin Karp algorithm?
DSA Rust
fn update_hash(old_hash: u64, left_char: u8, new_char: u8, h: u64, base: u64, prime: u64) -> u64 {
let mut new_hash = base * (old_hash - (left_char as u64) * h) + (new_char as u64);
new_hash %= prime;
new_hash
}
fn main() {
let old_hash = 6;
let left_char = b'a';
let new_char = b'd';
let h = 65536;
let base = 256;
let prime = 101;
let updated_hash = update_hash(old_hash, left_char, new_char, h, base, prime);
println!("Updated hash: {}", updated_hash);
}Attempts:
2 left
💡 Hint
Check the multiplication of large numbers and the type used.
✗ Incorrect
Multiplying large u64 values without checked arithmetic can overflow silently in Rust, but in debug mode it panics. The value h=65536 is large and multiplied by base and old_hash can overflow u64.
🚀 Application
advancedFind all occurrences of pattern using Rabin Karp
Given the text "abracadabra" and pattern "abra", which indices will the Rabin Karp algorithm report as matches?
Attempts:
2 left
💡 Hint
Check where the pattern "abra" appears exactly in the text.
✗ Incorrect
The pattern "abra" appears at index 0 and index 7 in "abracadabra".
🧠 Conceptual
expertWhy is a prime number used as modulus in Rabin Karp?
What is the main reason for using a prime number as the modulus in the Rabin Karp rolling hash function?
Attempts:
2 left
💡 Hint
Think about how modulus affects hash distribution.
✗ Incorrect
Using a prime modulus helps spread hash values more uniformly, reducing collisions and improving algorithm efficiency.
