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 Function
What is the output of the following Rust code that uses a naive string pattern matching function to find all occurrences of a pattern in a text?
DSA Rust
fn naive_pattern_search(text: &str, pattern: &str) -> Vec<usize> { let n = text.len(); let m = pattern.len(); let mut result = Vec::new(); for i in 0..=n - m { let mut j = 0; while j < m && &text[i + j..i + j + 1] == &pattern[j..j + 1] { j += 1; } if j == m { result.push(i); } } result } fn main() { let text = "abracadabra"; let pattern = "abra"; let positions = naive_pattern_search(text, pattern); println!("{:?}", positions); }
Attempts:
2 left
💡 Hint
Check where the substring "abra" appears exactly in "abracadabra".
✗ Incorrect
The pattern "abra" appears at index 0 and index 7 in the text "abracadabra". The naive search checks each position and finds matches only at these two indices.
❓ Predict Output
intermediateResult of Pattern Not Found
What will be the output of this Rust code when the pattern does not exist in the text?
DSA Rust
fn naive_pattern_search(text: &str, pattern: &str) -> Vec<usize> { let n = text.len(); let m = pattern.len(); let mut result = Vec::new(); for i in 0..=n - m { let mut j = 0; while j < m && &text[i + j..i + j + 1] == &pattern[j..j + 1] { j += 1; } if j == m { result.push(i); } } result } fn main() { let text = "hello world"; let pattern = "abc"; let positions = naive_pattern_search(text, pattern); println!("{:?}", positions); }
Attempts:
2 left
💡 Hint
If the pattern is not found, what does the function return?
✗ Incorrect
Since "abc" does not appear in "hello world", the function returns an empty vector indicating no matches.
🔧 Debug
advancedIdentify the Error in Naive Pattern Matching Code
What error will this Rust code produce when run, and why?
DSA Rust
fn naive_pattern_search(text: &str, pattern: &str) -> Vec<usize> { let n = text.len(); let m = pattern.len(); let mut result = Vec::new(); for i in 0..=n - m { let mut j = 0; while j < m && &text[i + j..i + j + 1] == &pattern[j..j + 1] { j += 1; } if j == m { result.push(i); } } result } fn main() { let text = "abc"; let pattern = "abcd"; let positions = naive_pattern_search(text, pattern); println!("{:?}", positions); }
Attempts:
2 left
💡 Hint
Check what happens when pattern length is greater than text length.
✗ Incorrect
The code tries to loop from 0 to n - m inclusive, but if m > n, this causes an underflow in the range, leading to a panic at runtime due to index out of bounds.
🧠 Conceptual
advancedTime Complexity of Naive Pattern Matching
What is the worst-case time complexity of the naive string pattern matching algorithm for a text of length n and a pattern of length m?
Attempts:
2 left
💡 Hint
Consider how many comparisons are done in the worst case.
✗ Incorrect
The naive algorithm checks for the pattern starting at each position in the text, and for each position, it may compare up to m characters, resulting in O(n * m) time complexity.
🚀 Application
expertFind All Occurrences of Overlapping Patterns
Given the naive pattern matching function, what will be the output when searching for the pattern "aa" in the text "aaaaa"?
DSA Rust
fn naive_pattern_search(text: &str, pattern: &str) -> Vec<usize> { let n = text.len(); let m = pattern.len(); let mut result = Vec::new(); for i in 0..=n - m { let mut j = 0; while j < m && &text[i + j..i + j + 1] == &pattern[j..j + 1] { j += 1; } if j == m { result.push(i); } } result } fn main() { let text = "aaaaa"; let pattern = "aa"; let positions = naive_pattern_search(text, pattern); println!("{:?}", positions); }
Attempts:
2 left
💡 Hint
Check all starting indices where the pattern "aa" appears, including overlapping ones.
✗ Incorrect
The pattern "aa" appears starting at indices 0, 1, 2, and 3 in the text "aaaaa" because overlapping matches are counted.
