Challenge - 5 Problems
KMP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of KMP prefix table computation
What is the output of the prefix table (lps array) after running the KMP preprocessing on the pattern
"ABABC"?DSA Rust
fn compute_lps(pattern: &str) -> Vec<usize> { let mut lps = vec![0; pattern.len()]; let mut length = 0; let mut i = 1; let chars: Vec<char> = pattern.chars().collect(); while i < pattern.len() { if chars[i] == chars[length] { length += 1; lps[i] = length; i += 1; } else { if length != 0 { length = lps[length - 1]; } else { lps[i] = 0; i += 1; } } } lps } fn main() { let pattern = "ABABC"; let lps = compute_lps(pattern); println!("{:?}", lps); }
Attempts:
2 left
💡 Hint
Remember the prefix table stores the length of the longest proper prefix which is also a suffix for each substring.
✗ Incorrect
The prefix table for "ABABC" is computed as follows: positions 0 and 1 have 0 because no proper prefix matches suffix; at position 2, 'A' matches start, so 1; at position 3, 'B' matches next, so 2; at position 4, no match, so 0.
❓ Predict Output
intermediateKMP search result index
What is the output index when searching for pattern
"ABCDABD" in text "ABC ABCDAB ABCDABCDABDE" using KMP algorithm?DSA Rust
fn kmp_search(text: &str, pattern: &str) -> Option<usize> { let lps = compute_lps(pattern); let text_chars: Vec<char> = text.chars().collect(); let pattern_chars: Vec<char> = pattern.chars().collect(); let mut i = 0; let mut j = 0; while i < text.len() { if text_chars[i] == pattern_chars[j] { i += 1; j += 1; if j == pattern.len() { return Some(i - j); } } else { if j != 0 { j = lps[j - 1]; } else { i += 1; } } } None } fn compute_lps(pattern: &str) -> Vec<usize> { let mut lps = vec![0; pattern.len()]; let mut length = 0; let mut i = 1; let chars: Vec<char> = pattern.chars().collect(); while i < pattern.len() { if chars[i] == chars[length] { length += 1; lps[i] = length; i += 1; } else { if length != 0 { length = lps[length - 1]; } else { lps[i] = 0; i += 1; } } } lps } fn main() { let text = "ABC ABCDAB ABCDABCDABDE"; let pattern = "ABCDABD"; match kmp_search(text, pattern) { Some(index) => println!("{}", index), None => println!("-1"), } }
Attempts:
2 left
💡 Hint
The pattern appears starting at index 15 in the text.
✗ Incorrect
The pattern "ABCDABD" first fully matches starting at index 15 in the given text.
🔧 Debug
advancedIdentify the error in KMP prefix table code
What error will the following Rust code produce when computing the prefix table for pattern
"AAAAB"?DSA Rust
fn compute_lps(pattern: &str) -> Vec<usize> { let mut lps = vec![0; pattern.len()]; let mut length = 0; let mut i = 1; let chars: Vec<char> = pattern.chars().collect(); while i <= pattern.len() { if chars[i] == chars[length] { length += 1; lps[i] = length; i += 1; } else { if length != 0 { length = lps[length - 1]; } else { lps[i] = 0; i += 1; } } } lps } fn main() { let pattern = "AAAAB"; let lps = compute_lps(pattern); println!("{:?}", lps); }
Attempts:
2 left
💡 Hint
Check the loop condition and indexing inside the loop.
✗ Incorrect
The loop condition uses <= pattern.len(), which causes index i to reach pattern.len(), leading to out of bounds access on chars[i].
🧠 Conceptual
advancedUnderstanding KMP algorithm time complexity
What is the time complexity of the KMP pattern matching algorithm for searching a pattern of length
m in a text of length n?Attempts:
2 left
💡 Hint
KMP avoids re-examining characters in the text.
✗ Incorrect
KMP preprocesses the pattern in O(m) and searches in O(n), so total time is O(n + m).
🚀 Application
expertFind all occurrences of pattern using KMP
Given the text
"ABABDABACDABABCABAB" and pattern "ABABCABAB", which option correctly lists all starting indices where the pattern occurs using KMP?DSA Rust
fn kmp_search_all(text: &str, pattern: &str) -> Vec<usize> { let lps = compute_lps(pattern); let text_chars: Vec<char> = text.chars().collect(); let pattern_chars: Vec<char> = pattern.chars().collect(); let mut i = 0; let mut j = 0; let mut result = Vec::new(); while i < text.len() { if text_chars[i] == pattern_chars[j] { i += 1; j += 1; if j == pattern.len() { result.push(i - j); j = lps[j - 1]; } } else { if j != 0 { j = lps[j - 1]; } else { i += 1; } } } result } fn compute_lps(pattern: &str) -> Vec<usize> { let mut lps = vec![0; pattern.len()]; let mut length = 0; let mut i = 1; let chars: Vec<char> = pattern.chars().collect(); while i < pattern.len() { if chars[i] == chars[length] { length += 1; lps[i] = length; i += 1; } else { if length != 0 { length = lps[length - 1]; } else { lps[i] = 0; i += 1; } } } lps } fn main() { let text = "ABABDABACDABABCABAB"; let pattern = "ABABCABAB"; let occurrences = kmp_search_all(text, pattern); println!("{:?}", occurrences); }
Attempts:
2 left
💡 Hint
The pattern appears once starting at index 10.
✗ Incorrect
The pattern "ABABCABAB" occurs only once in the text starting at index 10.
