What is the main goal of substring search algorithms?
To find the position(s) where a smaller string (pattern) appears inside a larger string (text).
Click to reveal answer
beginner
Explain the naive substring search approach.
It checks every possible position in the text to see if the pattern matches starting there, moving one by one until it finds a match or reaches the end.
Click to reveal answer
intermediate
What is the key idea behind the Knuth-Morris-Pratt (KMP) algorithm?
KMP uses a precomputed table to skip unnecessary comparisons by remembering how the pattern matches against itself, so it doesn't start over from scratch after a mismatch.
Click to reveal answer
intermediate
What does the 'prefix function' or 'lps array' represent in KMP?
It stores the length of the longest proper prefix of the pattern that is also a suffix for each position, helping to decide where to continue matching after a mismatch.
Click to reveal answer
advanced
Why is the Boyer-Moore algorithm efficient for substring search?
Because it compares the pattern from right to left and uses two rules to skip sections of the text, reducing the number of comparisons needed.
Click to reveal answer
What does the naive substring search algorithm do when a mismatch occurs?
AMoves the pattern one position to the right and starts matching from the beginning of the pattern.
BUses a precomputed table to skip some positions.
CStarts matching from the last matched character.
DStops searching immediately.
✗ Incorrect
The naive approach simply shifts the pattern by one and restarts matching from the first character.
In KMP, what is the purpose of the lps (longest prefix suffix) array?
ATo skip characters in the text after a mismatch.
BTo remember how much of the pattern matched so far to avoid rechecking.
CTo store the pattern characters.
DTo reverse the pattern.
✗ Incorrect
The lps array helps KMP know where to continue matching in the pattern after a mismatch.
Which substring search algorithm compares characters from right to left in the pattern?
ANaive search
BKMP algorithm
CRabin-Karp algorithm
DBoyer-Moore algorithm
✗ Incorrect
Boyer-Moore compares the pattern from right to left to optimize skipping.
What is the worst-case time complexity of the naive substring search?
AO(n log m)
BO(n + m)
CO(n * m)
DO(m^2)
✗ Incorrect
Naive search can check every position and every character, leading to O(n * m) in the worst case.
Which algorithm uses hashing to find substrings efficiently?
ARabin-Karp
BNaive search
CBoyer-Moore
DKMP
✗ Incorrect
Rabin-Karp uses hashing to quickly compare substring values.
Describe how the KMP algorithm improves substring search compared to the naive approach.
Think about how KMP avoids rechecking characters after a mismatch.
You got /4 concepts.
Explain the main differences between the naive, KMP, and Boyer-Moore substring search algorithms.
Focus on how each algorithm handles mismatches and skips.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of substring search algorithms in strings?
easy
A. To find if a smaller string exists inside a larger string
B. To sort the characters of a string alphabetically
C. To reverse the entire string
D. To count the total number of characters in a string
Solution
Step 1: Understand substring search purpose
Substring search algorithms check if a smaller string (pattern) appears inside a bigger string (text).
Step 2: Compare options with definition
Only To find if a smaller string exists inside a larger string describes finding a smaller string inside a larger one, which matches substring search.
Final Answer:
To find if a smaller string exists inside a larger string -> Option A
Hint: Substring search means finding a smaller string inside a bigger one [OK]
Common Mistakes:
Confusing substring search with sorting or reversing strings
Thinking substring search counts characters
Mixing substring search with string length calculation
2. Which of the following is the correct syntax to check if string pattern is a substring of string text in C?
easy
A. if (text.find(pattern)) { /* found */ }
B. if (text.contains(pattern)) { /* found */ }
C. if (pattern in text) { /* found */ }
D. if (strstr(text, pattern) != NULL) { /* found */ }
Solution
Step 1: Recall C substring search function
In C, strstr() returns a pointer to the first occurrence of pattern in text, or NULL if not found.
Step 2: Check each option's syntax
if (strstr(text, pattern) != NULL) { /* found */ } uses strstr correctly with NULL check. Options A, B, C use syntax from other languages or invalid in C.
Final Answer:
if (strstr(text, pattern) != NULL) { /* found */ } -> Option D
Quick Check:
C substring search uses strstr() [OK]
Hint: Use strstr() in C to find substring, check for NULL [OK]
Common Mistakes:
Using methods from other languages like contains() or find()
Not checking if strstr() returns NULL
Confusing order of arguments in strstr()
3. What will be the output of the following C code snippet?
strstr(text, "wor") returns pointer to first occurrence of "wor" in "hello world", which starts at index 6.
Step 2: Calculate pointer difference
Subtracting text pointer from pos gives index 6, which is printed.
Final Answer:
6 -> Option A
Quick Check:
Substring "wor" starts at index 6 [OK]
Hint: Pointer difference in strstr() gives substring start index [OK]
Common Mistakes:
Printing pointer value instead of index difference
Assuming substring starts at 0 or 3 incorrectly
Not checking if strstr() returns NULL before subtracting
4. Identify the error in the following C code for substring search:
char *find_substring(char *text, char *pattern) {
for (int i = 0; i <= strlen(text); i++) {
if (strncmp(&text[i], pattern, strlen(pattern)) == 0) {
return &text[i];
}
}
return NULL;
}
medium
A. Function should return int index, not pointer
B. strncmp() is used incorrectly; should use strcmp() instead
C. The loop condition should be 'i < strlen(text) - strlen(pattern) + 1' to avoid overflow
D. No error; code works correctly
Solution
Step 1: Analyze loop boundary
The loop runs from 0 to strlen(text), but comparing substrings beyond text length minus pattern length causes overflow.
Step 2: Correct loop condition
Loop should run until i <= strlen(text) - strlen(pattern) to safely compare substrings of pattern length.
Final Answer:
The loop condition should be 'i < strlen(text) - strlen(pattern) + 1' to avoid overflow -> Option C
Quick Check:
Loop boundary must prevent substring overflow [OK]
Hint: Loop till text length minus pattern length to avoid overflow [OK]
Common Mistakes:
Looping beyond valid substring start positions
Using strcmp instead of strncmp for partial comparison
Ignoring return type correctness
5. You want to find all occurrences of substring pat in string txt using C. Which approach correctly finds all start indices of pat in txt?
hard
A. Use strstr() once and return the first found position only
B. Use a loop with strstr() starting from last found position + 1 until strstr() returns NULL
C. Use strcmp() on entire text and pattern directly
D. Use strlen() to compare lengths and decide if substring exists
Solution
Step 1: Understand strstr() usage for multiple occurrences
strstr() returns pointer to first occurrence. To find all, repeatedly call strstr() starting after last found position.
Step 2: Evaluate options
Use a loop with strstr() starting from last found position + 1 until strstr() returns NULL correctly describes looping with strstr() advancing pointer. Use strstr() once and return the first found position only finds only first occurrence. Options C and D do not find all occurrences.
Final Answer:
Use a loop with strstr() starting from last found position + 1 until strstr() returns NULL -> Option B
Quick Check:
Loop with strstr() to find all occurrences [OK]
Hint: Loop with strstr() advancing pointer to find all matches [OK]
Common Mistakes:
Using strstr() only once for all matches
Using strcmp() which compares whole strings only
Relying on length checks without searching
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.