Challenge - 5 Problems
KMP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of KMP prefix table computation
What is the output array (prefix table) computed by the KMP algorithm for the pattern
"ABABAC"?DSA C#
int[] ComputePrefixTable(string pattern) { int[] prefix = new int[pattern.Length]; int j = 0; for (int i = 1; i < pattern.Length; i++) { while (j > 0 && pattern[i] != pattern[j]) { j = prefix[j - 1]; } if (pattern[i] == pattern[j]) { j++; prefix[i] = j; } } return prefix; } var result = ComputePrefixTable("ABABAC"); Console.WriteLine(string.Join(",", result));
Attempts:
2 left
💡 Hint
Remember the prefix table stores the length of the longest proper prefix which is also a suffix for each substring.
✗ Incorrect
The prefix table for "ABABAC" is computed step-by-step. The last value is 0 because 'C' does not continue the prefix pattern.
❓ Predict Output
intermediateKMP pattern search output
What is the output of the following KMP search code when searching for pattern
"ABABC" in text "ABABABCABABC"?DSA C#
List<int> KMPSearch(string text, string pattern) { int[] prefix = ComputePrefixTable(pattern); List<int> result = new List<int>(); int j = 0; for (int i = 0; i < text.Length; i++) { while (j > 0 && text[i] != pattern[j]) { j = prefix[j - 1]; } if (text[i] == pattern[j]) { j++; } if (j == pattern.Length) { result.Add(i - j + 1); j = prefix[j - 1]; } } return result; } var positions = KMPSearch("ABABABCABABC", "ABABC"); Console.WriteLine(string.Join(",", positions));
Attempts:
2 left
💡 Hint
Check where the pattern fully matches in the text.
✗ Incorrect
The pattern "ABABC" appears starting at indices 2 and 7 in the text.
🔧 Debug
advancedIdentify the error in prefix table computation
What error will the following code produce when computing the prefix table for pattern
"AABAACAABAA"?DSA C#
int[] ComputePrefixTable(string pattern) { int[] prefix = new int[pattern.Length]; int j = 0; for (int i = 1; i < pattern.Length; i++) { while (j > 0 && pattern[i] != pattern[j]) { j = prefix[j - 1]; } if (pattern[i] == pattern[j]) { j++; prefix[i] = j; } } return prefix; } var prefixTable = ComputePrefixTable("AABAACAABAA");
Attempts:
2 left
💡 Hint
Check the loop boundary conditions and array indexing.
✗ Incorrect
The loop uses i <= pattern.Length which causes index out of range when accessing pattern[i].
🧠 Conceptual
advancedWhy does KMP use the prefix table?
What is the main purpose of the prefix table in the KMP algorithm?
Attempts:
2 left
💡 Hint
Think about how KMP avoids repeating comparisons.
✗ Incorrect
The prefix table helps KMP skip unnecessary comparisons by remembering how far to jump back in the pattern.
🚀 Application
expertKMP search with overlapping patterns
Given the text
"AAAAA" and pattern "AAA", how many times does the KMP algorithm find the pattern in the text?Attempts:
2 left
💡 Hint
Consider overlapping occurrences of the pattern.
✗ Incorrect
The pattern "AAA" appears starting at indices 0, 1, and 2 in the text "AAAAA".
