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 Single Match
What is the output of this Go code that uses naive string pattern matching to find the first occurrence of a pattern in a text?
DSA Go
package main import "fmt" func naivePatternMatch(text, pattern string) int { n := len(text) m := len(pattern) for i := 0; i <= n-m; i++ { j := 0 for j < m && text[i+j] == pattern[j] { j++ } if j == m { return i } } return -1 } func main() { text := "abracadabra" pattern := "cada" fmt.Println(naivePatternMatch(text, pattern)) }
Attempts:
2 left
💡 Hint
Check where the pattern "cada" starts in the text "abracadabra".
✗ Incorrect
The pattern "cada" starts at index 4 in the text "abracadabra". The naive algorithm checks each position until it finds a full match.
❓ Predict Output
intermediateOutput when Pattern Not Found
What does the naive pattern matching function return when the pattern is not found in the text?
DSA Go
package main import "fmt" func naivePatternMatch(text, pattern string) int { n := len(text) m := len(pattern) for i := 0; i <= n-m; i++ { j := 0 for j < m && text[i+j] == pattern[j] { j++ } if j == m { return i } } return -1 } func main() { text := "hello world" pattern := "bye" fmt.Println(naivePatternMatch(text, pattern)) }
Attempts:
2 left
💡 Hint
If the pattern is not found, what index does the function return?
✗ Incorrect
The function returns -1 to indicate the pattern was 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 how many comparisons are done in the worst case for each position in the text.
✗ Incorrect
The naive algorithm checks each position in the text and compares up to m characters, leading to O(n * m) complexity.
🔧 Debug
advancedIdentify the Bug in Naive Pattern Matching Code
Which option contains a bug that causes incorrect pattern matching results?
DSA Go
func naivePatternMatch(text, pattern string) int { n := len(text) m := len(pattern) for i := 0; i < n-m; i++ { j := 0 for j < m && text[i+j] == pattern[j] { j++ } if j == m { return i } } return -1 }
Attempts:
2 left
💡 Hint
Check the loop boundary to ensure the pattern can fit at the last possible position.
✗ Incorrect
The outer loop must run until i <= n-m to check the last valid position where the pattern can fit.
🚀 Application
expertNumber of Matches Found by Naive Pattern Matching
Given the text "aaaaa" and the pattern "aa", how many matches will the naive pattern matching algorithm find if modified to find all occurrences?
DSA Go
package main import "fmt" func naivePatternMatchAll(text, pattern string) []int { n := len(text) m := len(pattern) var matches []int for i := 0; i <= n-m; i++ { j := 0 for j < m && text[i+j] == pattern[j] { j++ } if j == m { matches = append(matches, i) } } return matches } func main() { text := "aaaaa" pattern := "aa" fmt.Println(naivePatternMatchAll(text, pattern)) }
Attempts:
2 left
💡 Hint
Count overlapping occurrences of "aa" in "aaaaa".
✗ Incorrect
The pattern "aa" appears starting at indices 0,1,2,3 in the text "aaaaa", totaling 4 matches.
