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 Javascript
function computeLPSArray(pattern) {
const lps = 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 suffix for each substring.
✗ Incorrect
The prefix table (lps array) for pattern "ABABCABAB" is [0, 0, 1, 2, 0, 1, 2, 3, 4]. This array helps KMP skip unnecessary comparisons.
❓ Predict Output
intermediateKMP search output for a given text and pattern
What is the output (starting indices) of the KMP search for pattern
"ABAB" in text "ABABDABABABC"?DSA Javascript
function KMPSearch(pattern, text) {
const lps = Array(pattern.length).fill(0);
let j = 0;
const result = [];
function computeLPSArray(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++;
}
}
}
}
computeLPSArray(pattern);
let 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("ABAB", "ABABDABABABC"));Attempts:
2 left
💡 Hint
Look for exact matches of the pattern in the text and note their starting indices.
✗ Incorrect
The pattern "ABAB" appears starting at indices 0, 5, and 7 in the text "ABABDABABABC".
🧠 Conceptual
advancedUnderstanding the role of the LPS array in KMP
Which of the following best describes the role of the LPS (Longest Prefix Suffix) array in the KMP algorithm?
Attempts:
2 left
💡 Hint
Think about how KMP avoids rechecking characters after a mismatch.
✗ Incorrect
The LPS array helps KMP skip unnecessary comparisons by storing the longest prefix which is also suffix for each prefix of the pattern.
❓ Predict Output
advancedOutput of KMP search with overlapping patterns
What is the output of the KMP search for pattern
"AAA" in text "AAAAAA"?DSA Javascript
function KMPSearch(pattern, text) {
const lps = Array(pattern.length).fill(0);
let j = 0;
const result = [];
function computeLPSArray(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++;
}
}
}
}
computeLPSArray(pattern);
let 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("AAA", "AAAAAA"));Attempts:
2 left
💡 Hint
Consider overlapping occurrences of the pattern in the text.
✗ Incorrect
The pattern "AAA" appears starting at indices 0, 1, 2, and 3 in the text "AAAAAA" due to overlapping matches.
🧠 Conceptual
expertTime complexity of KMP algorithm
What is the worst-case time complexity of the KMP pattern matching algorithm and why?
Attempts:
2 left
💡 Hint
Consider how the LPS array helps avoid rechecking characters.
✗ Incorrect
KMP runs in O(n + m) time because it uses the LPS array to skip unnecessary comparisons, ensuring each character is processed at most once.
