Challenge - 5 Problems
Rabin Karp Master
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 JavaScript code that calculates the initial hash for the pattern 'abc' using a simple rolling hash?
DSA Javascript
const pattern = 'abc'; const base = 256; const mod = 101; let hash = 0; for (let i = 0; i < pattern.length; i++) { hash = (hash * base + pattern.charCodeAt(i)) % mod; } console.log(hash);
Attempts:
2 left
💡 Hint
Calculate hash step by step: hash = (hash * base + charCode) % mod for each character.
✗ Incorrect
The hash is computed as ((('a' * 256 + 'b') * 256) + 'c') mod 101. Calculating stepwise gives 90.
❓ Predict Output
intermediateRabin Karp pattern search output
What will be printed by the following code that searches for pattern 'test' in text 'this is a test text' using Rabin Karp?
DSA Javascript
function rabinKarp(text, pattern) {
const base = 256;
const mod = 101;
const m = pattern.length;
const n = text.length;
let pHash = 0, tHash = 0, h = 1;
for (let i = 0; i < m - 1; i++) h = (h * base) % mod;
for (let i = 0; i < m; i++) {
pHash = (pHash * base + pattern.charCodeAt(i)) % mod;
tHash = (tHash * base + text.charCodeAt(i)) % mod;
}
for (let i = 0; i <= n - m; i++) {
if (pHash === tHash) {
let match = true;
for (let j = 0; j < m; j++) {
if (text[i + j] !== pattern[j]) {
match = false;
break;
}
}
if (match) console.log(i);
}
if (i < n - m) {
tHash = (base * (tHash - text.charCodeAt(i) * h) + text.charCodeAt(i + m)) % mod;
if (tHash < 0) tHash += mod;
}
}
}
rabinKarp('this is a test text', 'test');Attempts:
2 left
💡 Hint
Check the index where 'test' starts in the text.
✗ Incorrect
The substring 'test' starts at index 10 in the text 'this is a test text'.
🧠 Conceptual
advancedWhy use modulo in Rabin Karp hashing?
Why does the Rabin Karp algorithm use modulo operation when calculating the rolling hash?
Attempts:
2 left
💡 Hint
Think about what happens if numbers get too big in calculations.
✗ Incorrect
Modulo keeps hash values within a manageable range and prevents overflow during calculations.
🔧 Debug
advancedIdentify the bug in rolling hash update
What is the bug in the following rolling hash update code snippet used in Rabin Karp?
DSA Javascript
tHash = (base * (tHash - text.charCodeAt(i) * h) + text.charCodeAt(i + m)) % mod;
Attempts:
2 left
💡 Hint
Check what happens if the subtraction makes tHash negative.
✗ Incorrect
If tHash becomes negative after subtraction, it must be adjusted by adding mod to keep it positive.
🚀 Application
expertNumber of hash collisions in Rabin Karp
Given a text of length 1000 and a pattern of length 5, using Rabin Karp with base=256 and mod=101, what is the maximum number of hash collisions that can occur during the search?
Attempts:
2 left
💡 Hint
Number of substrings checked is n - m + 1; collisions can happen at each substring except the first.
✗ Incorrect
There are 996 substrings of length 5 in text length 1000. Maximum collisions can happen at all except the first hash calculation, so 995.
