Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the initial hash of the pattern.
DSA Javascript
let patternHash = 0; for (let i = 0; i < pattern.length; i++) { patternHash = (patternHash * base + pattern.charCodeAt(i)) [1] mod; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of modulo causes incorrect hash values.
Forgetting modulo leads to very large numbers and errors.
✗ Incorrect
The modulo operator (%) is used to keep the hash value within a fixed range to avoid overflow.
2fill in blank
mediumComplete the code to update the rolling hash when sliding the window.
DSA Javascript
textHash = (textHash - text.charCodeAt(i) * power) [1] mod;
textHash = (textHash * base + text.charCodeAt(i + pattern.length)) % mod;Attempts:
3 left
💡 Hint
Common Mistakes
Not using modulo after subtraction causes negative hash values.
Using addition instead of modulo leads to wrong hash updates.
✗ Incorrect
Modulo is used to keep the hash value within range after subtraction.
3fill in blank
hardFix the error in the code that compares the substring with the pattern after hash match.
DSA Javascript
if (textHash === patternHash) { if (text.substr(i, pattern.length) [1] pattern) { result.push(i); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using == can cause unexpected results due to type coercion.
Using != or !== is incorrect for equality check.
✗ Incorrect
Use strict equality (===) to correctly compare strings in JavaScript.
4fill in blank
hardFill both blanks to correctly calculate the power used in rolling hash.
DSA Javascript
let power = 1; for (let i = 0; i < pattern.length - 1; i++) { power = (power [1] base) [2] mod; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication changes the power calculation.
Forgetting modulo causes large numbers and errors.
✗ Incorrect
Multiply power by base and then take modulo to keep the number within range.
5fill in blank
hardFill all three blanks to complete the Rabin Karp search function.
DSA Javascript
function rabinKarp(text, pattern) {
const base = 256;
const mod = 101;
let patternHash = 0, textHash = 0, power = 1;
let result = [];
for (let i = 0; i < pattern.length - 1; i++) {
power = (power [1] base) [2] mod;
}
for (let i = 0; i < pattern.length; i++) {
patternHash = (patternHash * base + pattern.charCodeAt(i)) % mod;
textHash = (textHash * base + text.charCodeAt(i)) % mod;
}
for (let i = 0; i <= text.length - pattern.length; i++) {
if (textHash === patternHash) {
if (text.substr(i, pattern.length) [3] pattern) {
result.push(i);
}
}
if (i < text.length - pattern.length) {
textHash = (textHash - text.charCodeAt(i) * power) % mod;
textHash = (textHash * base + text.charCodeAt(i + pattern.length)) % mod;
if (textHash < 0) textHash += mod;
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using loose equality (==) instead of strict equality (===).
Forgetting modulo after multiplication.
Not handling negative hash values after subtraction.
✗ Incorrect
Multiply power by base and modulo; use strict equality (===) to compare strings.
