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 for the pattern "abc" using a simple rolling hash?
DSA C#
using System; class Program { static void Main() { string pattern = "abc"; int prime = 101; int hash = 0; int power = 1; for (int i = pattern.Length - 1; i >= 0; i--) { hash += pattern[i] * power; power *= prime; } Console.WriteLine(hash); } }
Attempts:
2 left
💡 Hint
Calculate hash as sum of char codes times powers of prime starting from rightmost character.
✗ Incorrect
The hash is calculated as 'c'*1 + 'b'*101 + 'a'*101^2. Using ASCII codes: 'a'=97, 'b'=98, 'c'=99. So hash = 99 + 9898 + 989497 = 999494, which is option D.
❓ Predict Output
intermediateRabin Karp rolling hash update output
What is the output of this C# code snippet that updates the rolling hash when sliding the window from "abc" to "bcd"?
DSA C#
using System; class Program { static void Main() { string text = "abcd"; int prime = 101; int patternLength = 3; int power = 1; for (int i = 1; i < patternLength; i++) power *= prime; int hash = 0; for (int i = 0; i < patternLength; i++) { hash += text[i] * (int)Math.Pow(prime, patternLength - i - 1); } // Remove 'a', add 'd' hash = (hash - text[0] * power) * prime + text[3]; Console.WriteLine(hash); } }
Attempts:
2 left
💡 Hint
Use rolling hash formula: remove old char * highest power, multiply by prime, add new char.
✗ Incorrect
Initial hash for "abc" is 999494. Remove 'a' * 101^2 = 97*10201=989497, so 999494 - 989497 = 9997. Multiply by 101: 9997*101=1009697. Add 'd' (100): 1009797, which is option C.
🧠 Conceptual
advancedWhy use a prime number in Rabin Karp hashing?
Why is a prime number often chosen as the base (multiplier) in the Rabin Karp rolling hash algorithm?
Attempts:
2 left
💡 Hint
Think about how hash values distribute over possible inputs.
✗ Incorrect
Using a prime number as the base helps spread hash values more uniformly across the range, reducing collisions. It does not affect speed or guarantee even numbers or constant time.
🔧 Debug
advancedIdentify the error in this Rabin Karp implementation snippet
What error will this C# code produce when running the Rabin Karp algorithm?
DSA C#
int prime = 101; int patternLength = 3; int power = 1; for (int i = 0; i < patternLength; i++) { power *= prime; } // power is used as highest power of prime // Later in rolling hash update: hash = (hash - text[start] * power) * prime + text[start + patternLength];
Attempts:
2 left
💡 Hint
Check how many times power is multiplied and what it represents.
✗ Incorrect
The loop multiplies power prime times patternLength times, so power = prime^patternLength, but rolling hash needs prime^(patternLength-1). This causes logical error in hash update calculation.
🚀 Application
expertFind 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
Look for exact substring matches of "abra" in the text.
✗ Incorrect
The pattern "abra" appears starting at index 0 and index 7 in "abracadabra". Other indices do not match the full pattern.
