Challenge - 5 Problems
Naive Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Naive Pattern Matching on Simple Strings
What is the output of the following C code that uses naive pattern matching to find all occurrences of a pattern in a text?
DSA C
#include <stdio.h> #include <string.h> void naive_search(char *text, char *pattern) { int n = strlen(text); int m = strlen(pattern); for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) { if (text[i + j] != pattern[j]) break; } if (j == m) printf("%d ", i); } } int main() { char text[] = "ababcabcabababd"; char pattern[] = "ababd"; naive_search(text, pattern); return 0; }
Attempts:
2 left
💡 Hint
Check where the pattern "ababd" fully matches in the text.
✗ Incorrect
The pattern "ababd" appears only once starting at index 10 in the text. The naive search prints the starting indices of all matches.
❓ Predict Output
intermediate2:00remaining
Output when pattern is not present
What will be the output of this naive pattern matching code when the pattern does not exist in the text?
DSA C
#include <stdio.h> #include <string.h> void naive_search(char *text, char *pattern) { int n = strlen(text); int m = strlen(pattern); for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) { if (text[i + j] != pattern[j]) break; } if (j == m) printf("%d ", i); } } int main() { char text[] = "hello world"; char pattern[] = "bye"; naive_search(text, pattern); return 0; }
Attempts:
2 left
💡 Hint
If the pattern is not found, the function prints nothing.
✗ Incorrect
Since the pattern "bye" does not appear in "hello world", the loop never prints any index.
🧠 Conceptual
advanced1:30remaining
Time Complexity of Naive Pattern Matching
What is the worst-case time complexity of the naive pattern matching algorithm when searching a pattern of length m in a text of length n?
Attempts:
2 left
💡 Hint
Consider the nested loops: one over text and one over pattern.
✗ Incorrect
The naive algorithm checks each position in the text and compares up to m characters, leading to O(n*m) in the worst case.
🔧 Debug
advanced2:00remaining
Identify the Bug in Naive Pattern Matching Code
What error will occur when running this naive pattern matching code?
DSA C
#include <stdio.h> #include <string.h> void naive_search(char *text, char *pattern) { int n = strlen(text); int m = strlen(pattern); for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) { if (text[i + j] != pattern[j]) break; } if (j == m) printf("%d ", i); } } int main() { char text[] = "abc"; char pattern[] = "abc"; naive_search(text, pattern); return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition for i.
✗ Incorrect
The loop runs from i = 0 to i <= n - m, which is correct and includes the last valid index n - m, so it will find the match at index 0.
🚀 Application
expert2:00remaining
Number of Matches Found by Naive Pattern Matching
Given the text "aaaaaa" and pattern "aaa", how many matches will the naive pattern matching algorithm find?
Attempts:
2 left
💡 Hint
Consider overlapping matches in the text.
✗ Incorrect
The pattern "aaa" appears starting at indices 0, 1, 2, and 3 in the text "aaaaaa".
