Bird
0
0
DSA Cprogramming~20 mins

String Pattern Matching Naive in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Naive Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
ANo output
B0 5 10
C5 10
D10
Attempts:
2 left
💡 Hint
Check where the pattern "ababd" fully matches in the text.
Predict Output
intermediate
2: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;
}
ANo output
B0
C11
DError
Attempts:
2 left
💡 Hint
If the pattern is not found, the function prints nothing.
🧠 Conceptual
advanced
1: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?
AO(n^2)
BO(n * m)
CO(n + m)
DO(m^2)
Attempts:
2 left
💡 Hint
Consider the nested loops: one over text and one over pattern.
🔧 Debug
advanced
2: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;
}
ANo output
BInfinite loop
CSegmentation fault
DPrints 0 3 6
Attempts:
2 left
💡 Hint
Check the loop boundary condition for i.
🚀 Application
expert
2: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?
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Consider overlapping matches in the text.