Challenge - 5 Problems
KMP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of prefix table computation
What is the output of the prefix table (lps array) after running the KMP preprocessing on the pattern
"ABABCABAB"?DSA Typescript
function computeLPSArray(pattern: string): number[] {
const lps = new Array(pattern.length).fill(0);
let length = 0;
let i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
console.log(computeLPSArray("ABABCABAB"));Attempts:
2 left
💡 Hint
Remember the prefix table stores the length of the longest proper prefix which is also a suffix for each prefix of the pattern.
✗ Incorrect
The prefix table for "ABABCABAB" is computed by comparing characters and updating the longest prefix suffix length. The correct lps array is [0, 0, 1, 2, 0, 1, 2, 3, 4].
❓ Predict Output
intermediateKMP search output for pattern in text
What is the output of the following KMP search function when searching for pattern
"ABAB" in text "ABABDABAB"?DSA Typescript
function KMPSearch(text: string, pattern: string): number[] {
const lps = new Array(pattern.length).fill(0);
let j = 0;
const result: number[] = [];
// Preprocess the pattern
let length = 0;
let i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
// Search the pattern in text
i = 0;
while (i < text.length) {
if (pattern[j] === text[i]) {
i++;
j++;
}
if (j === pattern.length) {
result.push(i - j);
j = lps[j - 1];
} else if (i < text.length && pattern[j] !== text[i]) {
if (j !== 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return result;
}
console.log(KMPSearch("ABABDABAB", "ABAB"));Attempts:
2 left
💡 Hint
Check where the pattern "ABAB" fully matches in the text.
✗ Incorrect
The pattern "ABAB" appears starting at index 0 and index 5 in the text "ABABDABAB". So the output is [0, 5].
🧠 Conceptual
advancedUnderstanding the role of the LPS array
In the KMP algorithm, what is the main purpose of the LPS (Longest Prefix Suffix) array?
Attempts:
2 left
💡 Hint
Think about how KMP avoids rechecking characters after a mismatch.
✗ Incorrect
The LPS array helps the algorithm know how many characters can be skipped after a mismatch by storing the longest prefix which is also a suffix for each prefix of the pattern.
🔧 Debug
advancedIdentify the error in LPS computation
What error will the following LPS computation code produce when run with pattern
"AAAA"?DSA Typescript
function computeLPS(pattern: string): number[] {
const lps = new Array(pattern.length).fill(0);
let length = 0;
let i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
console.log(computeLPS("AAAA"));Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop condition uses <= pattern.length which causes access to pattern[pattern.length], which is undefined, leading to a TypeError when comparing characters.
🚀 Application
expertNumber of pattern occurrences using KMP
Given the text
"ABC ABCDAB ABCDABCDABDE" and pattern "ABCDABD", how many times does the pattern occur in the text using KMP search?DSA Typescript
function KMPSearchCount(text: string, pattern: string): number {
const lps = new Array(pattern.length).fill(0);
let j = 0;
let count = 0;
// Preprocess the pattern
let length = 0;
let i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
// Search the pattern in text
i = 0;
while (i < text.length) {
if (pattern[j] === text[i]) {
i++;
j++;
}
if (j === pattern.length) {
count++;
j = lps[j - 1];
} else if (i < text.length && pattern[j] !== text[i]) {
if (j !== 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return count;
}
console.log(KMPSearchCount("ABC ABCDAB ABCDABCDABDE", "ABCDABD"));Attempts:
2 left
💡 Hint
Count how many times the full pattern matches in the text.
✗ Incorrect
The pattern "ABCDABD" appears exactly once in the text at index 15.
