Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Palindrome Partitioning for "aab"
What is the output of the following Rust code that finds all palindrome partitions of the string "aab"?
DSA Rust
fn is_palindrome(s: &str) -> bool { s.chars().eq(s.chars().rev()) } fn backtrack(s: &str, start: usize, path: &mut Vec<String>, result: &mut Vec<Vec<String>>) { if start == s.len() { result.push(path.clone()); return; } for end in start + 1..=s.len() { let substring = &s[start..end]; if is_palindrome(substring) { path.push(substring.to_string()); backtrack(s, end, path, result); path.pop(); } } } fn partition(s: &str) -> Vec<Vec<String>> { let mut result = Vec::new(); let mut path = Vec::new(); backtrack(s, 0, &mut path, &mut result); result } fn main() { let partitions = partition("aab"); println!("{:?}", partitions); }
Attempts:
2 left
💡 Hint
Check all substrings and verify if they read the same forwards and backwards.
✗ Incorrect
The code finds all ways to split the string "aab" into substrings that are palindromes. "a", "a", "b" and "aa", "b" are the valid palindrome partitions.
🧠 Conceptual
intermediateNumber of Palindrome Partitions for "abc"
How many palindrome partitions does the string "abc" have?
Attempts:
2 left
💡 Hint
Each single character is a palindrome. Check if any longer substring is palindrome.
✗ Incorrect
Only the partition with each character alone is palindrome: ["a", "b", "c"]. No longer substrings like "ab" or "bc" or "abc" are palindromes.
🔧 Debug
advancedIdentify the Error in Palindrome Partitioning Code
What error will this Rust code produce when run?
DSA Rust
fn is_palindrome(s: &str) -> bool { s.chars().eq(s.chars().rev()) } fn backtrack(s: &str, start: usize, path: &mut Vec<String>, result: &mut Vec<Vec<String>>) { if start > s.len() { result.push(path.clone()); return; } for end in start + 1..=s.len() { let substring = &s[start..end]; if is_palindrome(substring) { path.push(substring.to_string()); backtrack(s, end, path, result); path.pop(); } } } fn partition(s: &str) -> Vec<Vec<String>> { let mut result = Vec::new(); let mut path = Vec::new(); backtrack(s, 0, &mut path, &mut result); result } fn main() { let partitions = partition("aab"); println!("{:?}", partitions); }
Attempts:
2 left
💡 Hint
Check the base case condition for recursion termination.
✗ Incorrect
The base case uses 'start > s.len()', but start can equal s.len() to stop. Using '>' causes infinite recursion leading to stack overflow.
❓ Predict Output
advancedOutput of Palindrome Partitioning for "aaa"
What is the output of the following Rust code that finds all palindrome partitions of the string "aaa"?
DSA Rust
fn is_palindrome(s: &str) -> bool { s.chars().eq(s.chars().rev()) } fn backtrack(s: &str, start: usize, path: &mut Vec<String>, result: &mut Vec<Vec<String>>) { if start == s.len() { result.push(path.clone()); return; } for end in start + 1..=s.len() { let substring = &s[start..end]; if is_palindrome(substring) { path.push(substring.to_string()); backtrack(s, end, path, result); path.pop(); } } } fn partition(s: &str) -> Vec<Vec<String>> { let mut result = Vec::new(); let mut path = Vec::new(); backtrack(s, 0, &mut path, &mut result); result } fn main() { let partitions = partition("aaa"); println!("{:?}", partitions); }
Attempts:
2 left
💡 Hint
Consider all palindrome substrings including single and multiple characters.
✗ Incorrect
All partitions where each substring is palindrome are: single letters, pairs "aa", and the whole "aaa".
🧠 Conceptual
expertTime Complexity of Palindrome Partitioning Using Backtracking
What is the time complexity of the palindrome partitioning algorithm using backtracking on a string of length n?
Attempts:
2 left
💡 Hint
Consider the number of partitions and palindrome checks.
✗ Incorrect
There are up to 2^(n-1) ways to partition a string. Each palindrome check can take O(n). So total is O(n * 2^n).
