Challenge - 5 Problems
Naive Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Naive Pattern Matching
What is the output of the following Java code that uses naive string pattern matching to find all occurrences of a pattern in a text?
DSA Java
public class NaivePatternMatching { public static void main(String[] args) { String text = "abracadabra"; String pattern = "abra"; for (int i = 0; i <= text.length() - pattern.length(); i++) { int j; for (j = 0; j < pattern.length(); j++) { if (text.charAt(i + j) != pattern.charAt(j)) { break; } } if (j == pattern.length()) { System.out.print(i + " "); } } } }
Attempts:
2 left
💡 Hint
Look for all starting indices where the pattern matches exactly in the text.
✗ Incorrect
The pattern "abra" appears twice in "abracadabra": starting at index 0 and index 7. The code prints these indices separated by spaces.
🧠 Conceptual
intermediateNumber of Comparisons in Worst Case
In the naive string pattern matching algorithm, what is the worst-case number of character comparisons 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 algorithm may compare all characters of the pattern.
✗ Incorrect
The naive algorithm checks each position in the text and compares up to m characters, leading to O(n * m) comparisons in the worst case.
🔧 Debug
advancedIdentify the Bug in Naive Pattern Matching Code
What error will the following Java code produce when run, and why?
DSA Java
public class NaiveMatch { public static void main(String[] args) { String text = "hello"; String pattern = "lo"; for (int i = 0; i < text.length() - pattern.length(); i++) { int j; for (j = 0; j < pattern.length(); j++) { if (text.charAt(i + j) != pattern.charAt(j)) { break; } } if (j == pattern.length()) { System.out.println(i); } } } }
Attempts:
2 left
💡 Hint
Check the loop boundary condition for the outer loop.
✗ Incorrect
The outer loop condition uses '<' instead of '<=' which causes it to skip the last valid starting index where the pattern could match.
❓ Predict Output
advancedOutput with Overlapping Patterns
What is the output of this Java code using naive pattern matching when the pattern overlaps in the text?
DSA Java
public class OverlapPattern { public static void main(String[] args) { String text = "aaaaa"; String pattern = "aaa"; for (int i = 0; i <= text.length() - pattern.length(); i++) { int j; for (j = 0; j < pattern.length(); j++) { if (text.charAt(i + j) != pattern.charAt(j)) { break; } } if (j == pattern.length()) { System.out.print(i + " "); } } } }
Attempts:
2 left
💡 Hint
Check all starting indices where the pattern "aaa" fully matches, including overlapping ones.
✗ Incorrect
The pattern "aaa" appears starting at indices 0, 1, and 2 in "aaaaa". The code prints all these indices separated by spaces.
🧠 Conceptual
expertWhy is Naive Pattern Matching Inefficient for Certain Texts?
Why does the naive string pattern matching algorithm perform poorly on texts with many repeated characters when searching for certain patterns?
Attempts:
2 left
💡 Hint
Think about how the algorithm compares characters when partial matches fail near the start.
✗ Incorrect
The naive algorithm restarts matching at the next position without using information from previous comparisons, causing repeated checks of the same characters especially in repetitive texts.
