Challenge - 5 Problems
String Pattern Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Naive Pattern Matching Positions
What is the output of the following C++ code that uses naive string pattern matching to find all occurrences of pattern
"aba" in text "ababaaba"?DSA C++
#include <iostream> #include <vector> #include <string> std::vector<int> naivePatternMatch(const std::string &text, const std::string &pattern) { std::vector<int> positions; int n = text.size(); int m = pattern.size(); for (int i = 0; i <= n - m; i++) { int j = 0; while (j < m && text[i + j] == pattern[j]) { j++; } if (j == m) { positions.push_back(i); } } return positions; } int main() { std::string text = "ababaaba"; std::string pattern = "aba"; std::vector<int> result = naivePatternMatch(text, pattern); for (int pos : result) { std::cout << pos << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check each substring of length equal to pattern and compare character by character.
✗ Incorrect
The pattern "aba" appears starting at indices 0 ("aba"), 2 ("aba"), and 5 ("aba") in the text "ababaaba".
🧠 Conceptual
intermediateNumber of Comparisons in Naive Pattern Matching
If the text length is 10 and the pattern length is 3, what is the maximum number of character comparisons the naive pattern matching algorithm will perform?
Attempts:
2 left
💡 Hint
Maximum comparisons happen when all characters match except the last one in each attempt.
✗ Incorrect
The naive algorithm tries all possible starting positions (10 - 3 + 1 = 8). For each, it compares up to 3 characters. In worst case, 3 comparisons per position, total 8*3=24.
🔧 Debug
advancedIdentify the Error in Naive Pattern Matching Code
What error will the following code produce when run?
DSA C++
#include <iostream> #include <string> int main() { std::string text = "hello"; std::string pattern = "lo"; for (int i = 0; i < text.size() - pattern.size(); i++) { int j = 0; while (j < pattern.size() && text[i + j] == pattern[j]) { j++; } if (j == pattern.size()) { std::cout << i << std::endl; } } return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition for the last possible starting index.
✗ Incorrect
The loop condition uses < instead of <=, so it misses the last valid starting index i=3 where "lo" matches in "hello". Hence, no output is printed.
❓ Predict Output
advancedOutput of Naive Pattern Matching with Overlapping Patterns
What is the output of the following code that finds all occurrences of pattern
"aa" in text "aaaaa"?DSA C++
#include <iostream> #include <vector> #include <string> std::vector<int> naivePatternMatch(const std::string &text, const std::string &pattern) { std::vector<int> positions; int n = text.size(); int m = pattern.size(); for (int i = 0; i <= n - m; i++) { int j = 0; while (j < m && text[i + j] == pattern[j]) { j++; } if (j == m) { positions.push_back(i); } } return positions; } int main() { std::string text = "aaaaa"; std::string pattern = "aa"; std::vector<int> result = naivePatternMatch(text, pattern); for (int pos : result) { std::cout << pos << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check for overlapping matches carefully.
✗ Incorrect
The pattern "aa" appears starting at indices 0, 1, 2, and 3 in the text "aaaaa".
🧠 Conceptual
expertTime Complexity of Naive Pattern Matching in Worst Case
What is the worst-case time complexity of the naive string pattern matching algorithm when text length is n and pattern length is m?
Attempts:
2 left
💡 Hint
Consider the nested loops: outer loop over text and inner loop over pattern.
✗ Incorrect
The naive algorithm checks each position in text and compares up to m characters of pattern, leading to O(n*m) comparisons in worst case.
