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 for Single Occurrence
What is the output of the following JavaScript code that uses naive string pattern matching to find the first occurrence of a pattern in a text?
DSA Javascript
function naiveSearch(text, pattern) {
for (let i = 0; i <= text.length - pattern.length; i++) {
let j;
for (j = 0; j < pattern.length; j++) {
if (text[i + j] !== pattern[j]) {
break;
}
}
if (j === pattern.length) {
return i;
}
}
return -1;
}
console.log(naiveSearch('abracadabra', 'cada'));Attempts:
2 left
💡 Hint
Check where the substring 'cada' starts in 'abracadabra'.
✗ Incorrect
The pattern 'cada' starts at index 3 in the text 'abracadabra'. The naive search returns the starting index of the first match.
❓ Predict Output
intermediateOutput when Pattern is Not Found
What will be the output of this naive pattern matching code when the pattern does not exist in the text?
DSA Javascript
function naiveSearch(text, pattern) {
for (let i = 0; i <= text.length - pattern.length; i++) {
let j;
for (j = 0; j < pattern.length; j++) {
if (text[i + j] !== pattern[j]) {
break;
}
}
if (j === pattern.length) {
return i;
}
}
return -1;
}
console.log(naiveSearch('hello world', 'bye'));Attempts:
2 left
💡 Hint
If the pattern is not found, what does the function return?
✗ Incorrect
The function returns -1 when the pattern is not found in the text.
🧠 Conceptual
advancedTime Complexity of Naive Pattern Matching
What is the worst-case time complexity of the naive string pattern matching algorithm when searching for a pattern of length m in a text of length n?
Attempts:
2 left
💡 Hint
Consider that for each position in the text, the pattern is checked character by character.
✗ Incorrect
The naive algorithm checks each position in the text and compares up to m characters, leading to O(n * m) time in the worst case.
❓ Predict Output
advancedOutput of Naive Pattern Matching with Multiple Matches
What will be the output of this code that finds all occurrences of a pattern in a text using naive pattern matching?
DSA Javascript
function naiveSearchAll(text, pattern) {
const result = [];
for (let i = 0; i <= text.length - pattern.length; i++) {
let j;
for (j = 0; j < pattern.length; j++) {
if (text[i + j] !== pattern[j]) {
break;
}
}
if (j === pattern.length) {
result.push(i);
}
}
return result;
}
console.log(naiveSearchAll('ababa', 'aba'));Attempts:
2 left
💡 Hint
Look for all starting indices where 'aba' appears in 'ababa'.
✗ Incorrect
The pattern 'aba' appears starting at indices 0 and 2 in 'ababa'.
🔧 Debug
expertIdentify the Bug in Naive Pattern Matching Implementation
The following code is intended to find the first occurrence of a pattern in a text using naive pattern matching. However, it sometimes returns incorrect results. What is the bug?
DSA Javascript
function naiveSearchBug(text, pattern) {
for (let i = 0; i < text.length - pattern.length; i++) {
let j = 0;
while (j < pattern.length && text[i + j] === pattern[j]) {
j++;
}
if (j === pattern.length) {
return i;
}
}
return -1;
}
console.log(naiveSearchBug('abracadabra', 'cada'));Attempts:
2 left
💡 Hint
Check the loop boundary for the outer loop to ensure all possible positions are checked.
✗ Incorrect
The outer loop stops too early because it uses < instead of <=, missing the last valid start index.
