Questions: String Matching: Naive and Optimized Approaches

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Why does the naive string matching algorithm perform poorly when searching for 'AAAB' in 'AAAAAAAAB'?

AThe pattern is too long relative to the text for naive matching to handle
BNaive matching cannot handle repeated characters
CAt each mismatch, the algorithm resets to position 0 of the pattern and slides one character forward — discarding all information from the previous partial match
DThe naive algorithm has O(m log n) complexity, which is worse than O(nm) for this input
Question 2 Multiple Choice

KMP's failure function (prefix function) stores, for each position j in the pattern, the length of the longest proper prefix of pattern[0..j] that is also a suffix. How does this help during matching?

AIt tells the algorithm how many text characters to skip forward after a complete match
BIt predicts how many mismatches will occur for a given text
CAfter a mismatch at pattern position j, it tells the algorithm where to resume in the pattern — preserving already-matched characters without re-examining text characters
DIt precomputes the hash of each pattern prefix to enable O(1) comparison
Question 3 True / False

In the worst case, KMP may still re-examine the same text character multiple times.

TTrue
FFalse
Question 4 True / False

Boyer-Moore is generally faster than KMP in practice because it can skip large portions of the text without examining every character.

TTrue
FFalse
Question 5 Short Answer

Explain the key insight that makes KMP more efficient than naive string matching. What does it avoid doing that the naive algorithm does?

Think about your answer, then reveal below.