Challenge - 5 Problems
Naive Pattern Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Naive Pattern Matching with Overlapping Matches
What is the output of the following C# code that uses naive string pattern matching to find all occurrences of a pattern in a text, including overlapping matches?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int> NaivePatternMatch(string text, string pattern) { List<int> positions = new List<int>(); int n = text.Length; int m = pattern.Length; 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) { positions.Add(i); } } return positions; } static void Main() { string text = "ababa"; string pattern = "aba"; var result = NaivePatternMatch(text, pattern); Console.WriteLine(string.Join(",", result)); } }
Attempts:
2 left
💡 Hint
Check how the inner loop compares characters and how overlapping matches are handled.
✗ Incorrect
The naive pattern matching checks every position in the text where the pattern can fit. For text "ababa" and pattern "aba", matches occur at index 0 ("aba") and index 2 ("aba"), including overlapping occurrences.
❓ Predict Output
intermediateResult of Naive Pattern Matching with No Matches
What does the following C# code print when the pattern does not appear in the text?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int> NaivePatternMatch(string text, string pattern) { List<int> positions = new List<int>(); int n = text.Length; int m = pattern.Length; 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) { positions.Add(i); } } return positions; } static void Main() { string text = "hello world"; string pattern = "bye"; var result = NaivePatternMatch(text, pattern); Console.WriteLine(string.Join(",", result)); } }
Attempts:
2 left
💡 Hint
Consider what the list contains if no matches are found.
✗ Incorrect
If the pattern is not found, the positions list remains empty. Joining an empty list with commas results in an empty string.
🧠 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 the nested loops: one over the text and one over the pattern.
✗ Incorrect
The naive algorithm checks each position in the text (n positions) and for each position compares up to m characters, resulting in O(n * m) time complexity.
🔧 Debug
advancedIdentify the Bug in Naive Pattern Matching Code
What issue will occur when running the following naive pattern matching code and why?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int> NaivePatternMatch(string text, string pattern) { List<int> positions = new List<int>(); int n = text.Length; int m = pattern.Length; 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) { positions.Add(i); } } return positions; } static void Main() { string text = "abcde"; string pattern = "de"; var result = NaivePatternMatch(text, pattern); Console.WriteLine(string.Join(",", result)); } }
Attempts:
2 left
💡 Hint
Check the loop boundary condition for the outer loop.
✗ Incorrect
The outer loop runs with condition i < n - m, which excludes the last valid start index i=3 where the pattern "de" matches text[3:5]. The code outputs empty string instead of "3".
🚀 Application
expertNumber of Matches Found by Naive Pattern Matching
Given the text "aaaaa" and pattern "aa", how many matches will the naive pattern matching algorithm find?
Attempts:
2 left
💡 Hint
Consider overlapping matches carefully.
✗ Incorrect
The pattern "aa" appears starting at indices 0, 1, 2, and 3 in the text "aaaaa". So there are 4 matches including overlaps.
