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 prefix[] = {0, 0, 0, 0, 0, 0}; // After running KMP prefix computation on "ABABAC" // prefix array stores longest prefix suffix lengths // What is the final prefix array?
Attempts:
2 left
💡 Hint
Remember prefix[i] stores the length of the longest proper prefix which is also suffix for substring pattern[0..i].
✗ Incorrect
The prefix table for "ABABAC" is computed step-by-step. The longest prefix suffix lengths are [0,0,1,2,3,0].
❓ Predict Output
intermediateKMP search output for pattern in text
Given the text
"ABC ABCDAB ABCDABCDABDE" and pattern "ABCDABD", what is the starting index of the first match found by the KMP algorithm?DSA C++
// Text: "ABC ABCDAB ABCDABCDABDE" // Pattern: "ABCDABD" // What is the index of the first occurrence of pattern in text using KMP?
Attempts:
2 left
💡 Hint
KMP finds the pattern starting at index 15 in the text.
✗ Incorrect
The pattern "ABCDABD" first appears starting at index 15 in the text string.
🔧 Debug
advancedIdentify the bug in KMP prefix computation code snippet
What error will the following C++ code produce when computing the prefix table for pattern
"AAACAAAA"?DSA C++
int prefix[8] = {0}; int j = 0; for (int i = 1; i < 8; i++) { while (j > 0 && pattern[i] != pattern[j]) { j = prefix[j - 1]; } if (pattern[i] == pattern[j]) { j++; } prefix[i] = j; }
Attempts:
2 left
💡 Hint
Check if all variables are declared and initialized properly.
✗ Incorrect
The code snippet uses 'pattern' without declaring or initializing it, causing a compilation error.
❓ Predict Output
advancedKMP algorithm behavior on repeated pattern
What is the output of the KMP search when searching for pattern
"AAAA" in text "AAAAAAAA"? Output the starting indices of all matches found.DSA C++
// Text: "AAAAAAAA" // Pattern: "AAAA" // Output all starting indices where pattern matches text using KMP
Attempts:
2 left
💡 Hint
Overlapping matches are found in KMP, so all valid starting indices are included.
✗ Incorrect
The pattern "AAAA" appears starting at indices 0,1,2,3,4 in the text "AAAAAAAA" due to overlapping occurrences.
🧠 Conceptual
expertWhy does KMP algorithm run in linear time?
Which of the following best explains why the KMP algorithm runs 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 KMP avoids repeating work on the text.
✗ Incorrect
KMP uses the prefix table to skip unnecessary comparisons, ensuring each character in the text is examined at most once.
