Challenge - 5 Problems
KMP Mastery Badge
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 of the prefix table (lps array) after running the KMP preprocessing on the pattern
"ABABCABAB"?DSA Java
public class KMP { public static int[] computeLPSArray(String pat) { int M = pat.length(); int[] lps = new int[M]; int len = 0; int i = 1; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } return lps; } public static void main(String[] args) { int[] lps = computeLPSArray("ABABCABAB"); for (int val : lps) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Remember the prefix table stores the length of the longest proper prefix which is also suffix for each prefix of the pattern.
✗ Incorrect
The prefix table for pattern "ABABCABAB" is computed step-by-step, resulting in the array [0, 0, 1, 2, 0, 1, 2, 3, 4]. This array helps the KMP algorithm skip unnecessary comparisons.
❓ Predict Output
intermediateKMP search output for pattern in text
What is the output of the following KMP search code when searching for pattern
"ABABCABAB" in text "ABABDABABCABAB"?DSA Java
public class KMP { public static int[] computeLPSArray(String pat) { int M = pat.length(); int[] lps = new int[M]; int len = 0; int i = 1; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } return lps; } public static void KMPSearch(String pat, String txt) { int M = pat.length(); int N = txt.length(); int[] lps = computeLPSArray(pat); int i = 0; int j = 0; while (i < N) { if (pat.charAt(j) == txt.charAt(i)) { i++; j++; } if (j == M) { System.out.print((i - j) + " "); j = lps[j - 1]; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } } public static void main(String[] args) { KMPSearch("ABABCABAB", "ABABDABABCABAB"); } }
Attempts:
2 left
💡 Hint
The pattern appears once starting at index 5 in the text.
✗ Incorrect
The pattern "ABABCABAB" occurs once in the text "ABABDABABCABAB" starting at index 5. The KMP algorithm prints the starting index of each match.
🔧 Debug
advancedIdentify the bug in KMP prefix table computation
What error will the following code produce when computing the prefix table for pattern
"AAAA"?DSA Java
public static int[] computeLPSArray(String pat) { int M = pat.length(); int[] lps = new int[M]; int len = 0; int i = 1; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } return lps; }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop condition uses i <= M, but array indices go from 0 to M-1. Accessing pat.charAt(i) when i == M causes ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedUnderstanding KMP algorithm efficiency
Why does the KMP algorithm run in O(N + M) time, where N is the length of the text and M is the length of the pattern?
Attempts:
2 left
💡 Hint
Think about how the prefix table helps skip unnecessary comparisons.
✗ Incorrect
KMP uses the prefix table (lps array) to know how many characters can be skipped after a mismatch, so it never moves backwards in the text, resulting in linear time complexity O(N + M).
🚀 Application
expertFind all occurrences of pattern in text using KMP
Given the text
"ABC ABCDAB ABCDABCDABDE" and pattern "ABCDABD", what are the starting indices of all occurrences of the pattern in the text using KMP?DSA Java
public class KMP { public static int[] computeLPSArray(String pat) { int M = pat.length(); int[] lps = new int[M]; int len = 0; int i = 1; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } return lps; } public static void KMPSearch(String pat, String txt) { int M = pat.length(); int N = txt.length(); int[] lps = computeLPSArray(pat); int i = 0; int j = 0; while (i < N) { if (pat.charAt(j) == txt.charAt(i)) { i++; j++; } if (j == M) { System.out.print((i - j) + " "); j = lps[j - 1]; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } } public static void main(String[] args) { KMPSearch("ABCDABD", "ABC ABCDAB ABCDABCDABDE"); } }
Attempts:
2 left
💡 Hint
Check carefully where the pattern fully matches in the text.
✗ Incorrect
The pattern "ABCDABD" appears twice in the text starting at indices 4 and 11. The KMP algorithm prints these starting indices.
