Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the initial hash of the pattern.
DSA Typescript
let patternHash = 0; for (let i = 0; i < pattern.length; i++) { patternHash = (patternHash * base + pattern.charCodeAt(i)) % [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pattern.length or text.length as modulus causes incorrect hash calculations.
Not using modulus leads to integer overflow.
✗ Incorrect
The modulus with a prime number keeps the hash values manageable and reduces collisions.
2fill in blank
mediumComplete the code to update the rolling hash when sliding the window over the text.
DSA Typescript
textHash = (textHash - text.charCodeAt(i - 1) * [1]) * base + text.charCodeAt(i + pattern.length - 1); textHash %= prime; if (textHash < 0) textHash += prime;
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting old char code multiplied by base instead of base^(pattern.length - 1).
Not adjusting negative hash values after modulus.
✗ Incorrect
We subtract the old character's weighted value using base raised to (pattern.length - 1).
3fill in blank
hardFix the error in the hash comparison condition to check for a match.
DSA Typescript
if (patternHash === [1]) { // Verify characters one by one }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing patternHash with itself always returns true.
Comparing with base or a single character code is wrong.
✗ Incorrect
We compare the pattern's hash with the current text window's hash to detect potential matches.
4fill in blank
hardFill both blanks to complete the loop that slides over the text and updates the hash.
DSA Typescript
for (let i = 1; i <= text.length - pattern.length; i++) { textHash = (textHash - text.charCodeAt(i - 1) * [1]) * base + text.charCodeAt(i + pattern.length - 1); textHash %= [2]; if (textHash < 0) textHash += [2]; // Check for match }
Attempts:
3 left
💡 Hint
Common Mistakes
Using base or pattern.length as modulus causes wrong hash values.
Not adding prime when hash is negative leads to errors.
✗ Incorrect
We subtract the old char weighted by basePow and take modulus with prime to keep hash in range.
5fill in blank
hardFill all three blanks to complete the function that returns all starting indices of pattern matches in text.
DSA Typescript
function rabinKarp(text: string, pattern: string): number[] {
const result: number[] = [];
const base = 256;
const prime = 101;
let patternHash = 0, textHash = 0, basePow = 1;
for (let i = 0; i < pattern.length - 1; i++) {
basePow = (basePow * base) % prime;
}
for (let i = 0; i < pattern.length; i++) {
patternHash = (patternHash * base + pattern.charCodeAt(i)) % prime;
textHash = (textHash * base + text.charCodeAt(i)) % prime;
}
for (let i = 0; i <= text.length - pattern.length; i++) {
if (patternHash === textHash) {
let match = true;
for (let j = 0; j < pattern.length; j++) {
if (text[i + j] !== pattern[j]) {
match = false;
break;
}
}
if (match) result.push([1]);
}
if (i < text.length - pattern.length) {
textHash = (textHash - text.charCodeAt(i) * [2]) * base + text.charCodeAt(i + pattern.length);
textHash %= [3];
if (textHash < 0) textHash += [3];
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Pushing pattern.length or base instead of current index.
Using base or pattern.length as modulus causes wrong hash updates.
✗ Incorrect
We push the current index i when a match is found, subtract old char weighted by basePow, and use prime as modulus.
