Bird
0
0
DSA Cprogramming~20 mins

KMP Pattern Matching Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
KMP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of KMP prefix table computation
What is the output array (prefix table) computed by the KMP algorithm for the pattern "ABABAC"?
DSA C
int pattern[] = { 'A', 'B', 'A', 'B', 'A', 'C' };
int prefix[] = {0, 0, 0, 0, 0, 0};
// KMP prefix table computation logic here
// Output prefix array after computation
A[0, 1, 2, 3, 4, 0]
B[0, 1, 0, 1, 2, 0]
C[0, 0, 1, 2, 0, 1]
D[0, 0, 1, 2, 3, 0]
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.
Predict Output
intermediate
2:00remaining
KMP search output for given text and pattern
What is the output (starting indices) of the KMP search for pattern "ABABC" in text "ABABABCABABC"?
DSA C
char text[] = "ABABABCABABC";
char pattern[] = "ABABC";
// KMP search logic
// Print all starting indices where pattern is found
A[0, 2, 7]
B[2, 7]
C[0, 5]
D[1, 6]
Attempts:
2 left
💡 Hint
Check carefully where the pattern fully matches in the text.
🔧 Debug
advanced
2:00remaining
Identify the bug in prefix table computation
Given the following prefix table computation code snippet, what error will it cause when run with pattern "AAAAB"?
DSA C
int computePrefix(char* pattern, int length, int* prefix) {
    int j = 0;
    prefix[0] = 0;
    for (int i = 1; i < length; i++) {
        while (j > 0 && pattern[i] != pattern[j]) {
            j = prefix[j - 1];
        }
        if (pattern[i] == pattern[j]) {
            j++;
        }
        prefix[i] = j;
    }
    return 0;
}
ARuntime error due to infinite loop
BSegmentation fault due to invalid prefix index access
CCorrect prefix table computed without error
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check the line where j is updated inside the while loop.
🧠 Conceptual
advanced
1:00remaining
Time complexity of KMP algorithm
What is the time complexity of the KMP pattern matching algorithm for a text of length n and pattern of length m?
AO(n + m)
BO(n * m)
CO(n^2)
DO(m^2)
Attempts:
2 left
💡 Hint
KMP avoids rechecking characters by using the prefix table.
🚀 Application
expert
2:00remaining
Using KMP to find the longest prefix which is also suffix
Given a string s, how can the KMP prefix table be used to find the length of the longest prefix of s which is also a suffix (excluding the whole string)?
AMaximum value in the prefix table minus one gives the length
BThe first value in the prefix table gives the length of the longest prefix-suffix
CThe last value in the prefix table gives the length of the longest prefix-suffix
DSum of all values in the prefix table gives the length
Attempts:
2 left
💡 Hint
The prefix table stores lengths of longest prefix-suffix for each prefix of the string.