What does KMP stand for in the KMP Pattern Matching Algorithm?
KMP stands for Knuth-Morris-Pratt, the last names of the three computer scientists who developed this efficient pattern matching algorithm.
Click to reveal answer
beginner
What is the main advantage of the KMP algorithm over the naive pattern matching approach?
KMP avoids rechecking characters that have already been matched by using a precomputed 'Longest Prefix Suffix' (LPS) array, making it faster especially for large texts and patterns.
Click to reveal answer
intermediate
What is the purpose of the LPS (Longest Prefix Suffix) array in KMP?
The LPS array stores the length of the longest proper prefix of the pattern that is also a suffix for each prefix of the pattern. It helps to decide where to continue matching after a mismatch.
Click to reveal answer
intermediate
In KMP, what happens when a mismatch occurs after some characters have matched?
Instead of restarting from the next character in the text, KMP uses the LPS array to shift the pattern to the right position, skipping unnecessary comparisons.
Click to reveal answer
intermediate
What is the time complexity of the KMP Pattern Matching Algorithm?
The time complexity is O(n + m), where n is the length of the text and m is the length of the pattern, because each character is processed at most twice.
Click to reveal answer
What does the LPS array in KMP represent?
ALength of the pattern
BLongest prefix which is also suffix for pattern prefixes
CPositions of mismatches in the text
DNumber of matches found
✗ Incorrect
The LPS array stores the longest proper prefix which is also a suffix for each prefix of the pattern.
What is the main benefit of using the KMP algorithm?
AIt reduces the number of character comparisons
BIt uses less memory than naive search
CIt sorts the text before searching
DIt works only on numeric data
✗ Incorrect
KMP reduces redundant comparisons by using the LPS array to skip characters after mismatches.
If a mismatch occurs at pattern index 3, where does KMP shift the pattern next?
ATo the end of the pattern
BTo the start of the pattern
CTo the index given by LPS[2]
DOne position to the right
✗ Incorrect
KMP uses the LPS value of the previous index to decide the next position to continue matching.
What is the worst-case time complexity of KMP?
AO(n + m)
BO(n * m)
CO(n^2)
DO(m^2)
✗ Incorrect
KMP runs in linear time relative to the sum of text and pattern lengths.
Which of the following is NOT a step in the KMP algorithm?
AReport matches when all pattern characters match
BCompare characters and shift pattern using LPS on mismatch
CPrecompute the LPS array
DSort the text before searching
✗ Incorrect
KMP does not sort the text; it uses the LPS array to efficiently search.
Explain how the LPS array is constructed in the KMP algorithm and why it is important.
Think about how pattern prefixes relate to suffixes and how this helps avoid rechecking characters.
You got /3 concepts.
Describe the process of pattern matching in KMP when a mismatch occurs and how it differs from naive search.
Focus on how KMP uses previous knowledge to continue matching without starting over.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the KMP (Knuth-Morris-Pratt) algorithm in pattern matching?
easy
A. To find occurrences of a pattern in a text efficiently without rechecking characters
B. To sort the characters of the pattern alphabetically
C. To reverse the text before searching
D. To count the number of vowels in the text
Solution
Step 1: Understand KMP's goal
KMP algorithm is designed to search for a pattern inside a text efficiently.
Step 2: Identify how it works
It avoids rechecking characters by using a prefix table to skip unnecessary comparisons.
Final Answer:
To find occurrences of a pattern in a text efficiently without rechecking characters -> Option A
Quick Check:
KMP purpose = efficient pattern search [OK]
Hint: KMP skips repeated checks using prefix info [OK]
Common Mistakes:
Thinking KMP sorts or modifies the text
Confusing KMP with simple substring search
Assuming KMP counts characters or vowels
2. Which of the following is the correct way to declare the prefix (lps) array in C++ for KMP algorithm?
easy
A. int lps[pattern_length];
B. int* lps = new int[pattern_length];
C. int lps = new int[pattern_length];
D. int lps[] = pattern_length;
Solution
Step 1: Understand dynamic array allocation in C++
To create an array dynamically, use pointer with new keyword: int* lps = new int[size];
Step 2: Check options
int* lps = new int[pattern_length]; correctly uses pointer and new to allocate array of given length.
Final Answer:
int* lps = new int[pattern_length]; -> Option B
Quick Check:
Dynamic array = int* lps = new int[size]; [OK]
Hint: Use int* with new[] for dynamic arrays in C++ [OK]
Common Mistakes:
Using int lps = new int[] (wrong type)
Declaring fixed size without initialization
Incorrect array syntax like int lps[] = size
3. What will be the output of the following code snippet that uses KMP to find pattern occurrences?
Assuming kmpSearch returns all starting indices of pattern in text.
medium
A. 10
B. 0 5 10
C. 5 10
D. No output
Solution
Step 1: Identify pattern occurrences in text
Check where "ababd" appears in "ababcabcabababd". It appears starting at index 10 only.
Step 2: Confirm output format
kmpSearch returns vector of indices; printing them separated by space outputs "10 ".
Final Answer:
10 -> Option A
Quick Check:
Pattern found at index 10 only [OK]
Hint: Scan text for exact pattern start index [OK]
Common Mistakes:
Assuming multiple matches without checking text
Confusing substring indices
Printing no output if pattern not found
4. Consider this buggy code snippet for computing the LPS array in KMP:
void computeLPSArray(string pat, int M, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len]; // Bug here
} else {
lps[i] = 0;
i++;
}
}
}
}
What is the bug and how to fix it?
medium
A. Replace while with for loop
B. Initialize len to 1 instead of 0
C. Remove else block completely
D. Change len = lps[len]; to len = lps[len - 1];
Solution
Step 1: Identify incorrect index in LPS update
When mismatch occurs and len != 0, len should be updated to lps[len-1], not lps[len].
Step 2: Correct the assignment
Change len = lps[len]; to len = lps[len - 1]; to avoid out-of-bound and logical error.
Final Answer:
Change len = lps[len]; to len = lps[len - 1]; -> Option D
Quick Check:
LPS update uses len-1 index [OK]
Hint: Use len-1 index when updating len in LPS [OK]
Common Mistakes:
Using len index directly causes wrong LPS values
Changing loop type unnecessarily
Removing else block breaks logic
5. Given a text and multiple patterns, which approach best uses KMP to find all patterns efficiently?
hard
A. Run KMP separately for each pattern on the text
B. Concatenate all patterns and run KMP once
C. Build a trie of patterns and run Aho-Corasick algorithm
D. Use KMP only on the longest pattern
Solution
Step 1: Understand KMP's limitation
KMP efficiently finds one pattern in text but running it repeatedly for many patterns is inefficient.
Step 2: Identify better multi-pattern method
Building a trie and using Aho-Corasick algorithm finds all patterns simultaneously efficiently.
Final Answer:
Build a trie of patterns and run Aho-Corasick algorithm -> Option C
Quick Check:
Multi-pattern search = Aho-Corasick [OK]
Hint: Use Aho-Corasick for multiple patterns [OK]
Common Mistakes:
Running KMP repeatedly wastes time
Concatenating patterns confuses matching
Ignoring specialized multi-pattern algorithms
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.