Challenge - 5 Problems
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Two Pointer Palindrome Check
What is the output of this Rust code that checks if a string is a palindrome ignoring non-alphanumeric characters and case?
DSA Rust
fn is_palindrome(s: &str) -> bool { let s = s.to_lowercase(); let bytes = s.as_bytes(); let (mut left, mut right) = (0, bytes.len() - 1); while left < right { if !bytes[left].is_ascii_alphanumeric() { left += 1; continue; } if !bytes[right].is_ascii_alphanumeric() { right -= 1; continue; } if bytes[left] != bytes[right] { return false; } left += 1; right -= 1; } true } fn main() { let test_str = "A man, a plan, a canal: Panama"; println!("{}", is_palindrome(test_str)); }
Attempts:
2 left
💡 Hint
Think about how the two pointers skip non-alphanumeric characters and compare letters ignoring case.
✗ Incorrect
The code converts the string to lowercase and uses two pointers to compare characters from start and end, skipping non-alphanumeric characters. The given string is a well-known palindrome phrase ignoring punctuation and case, so the function returns true.
❓ Predict Output
intermediateResult of Palindrome Check on Non-Palindrome String
What will the output be when running this Rust palindrome check code on the string "Hello, World!"?
DSA Rust
fn is_palindrome(s: &str) -> bool { let s = s.to_lowercase(); let bytes = s.as_bytes(); let (mut left, mut right) = (0, bytes.len() - 1); while left < right { if !bytes[left].is_ascii_alphanumeric() { left += 1; continue; } if !bytes[right].is_ascii_alphanumeric() { right -= 1; continue; } if bytes[left] != bytes[right] { return false; } left += 1; right -= 1; } true } fn main() { let test_str = "Hello, World!"; println!("{}", is_palindrome(test_str)); }
Attempts:
2 left
💡 Hint
Check if the characters match when ignoring punctuation and case.
✗ Incorrect
The string "Hello, World!" is not a palindrome because the characters do not match when compared from start and end ignoring punctuation and case. The function returns false.
🔧 Debug
advancedIdentify the Bug in Palindrome Two Pointer Code
This Rust code attempts to check if a string is a palindrome ignoring non-alphanumeric characters and case. What error will it produce when run?
DSA Rust
fn is_palindrome(s: &str) -> bool { let s = s.to_lowercase(); let bytes = s.as_bytes(); let (mut left, mut right) = (0, bytes.len()); while left < right { if !bytes[left].is_ascii_alphanumeric() { left += 1; continue; } if !bytes[right].is_ascii_alphanumeric() { right -= 1; continue; } if bytes[left] != bytes[right] { return false; } left += 1; right -= 1; } true } fn main() { let test_str = "racecar"; println!("{}", is_palindrome(test_str)); }
Attempts:
2 left
💡 Hint
Check how the right pointer is initialized and used.
✗ Incorrect
The right pointer is initialized to bytes.len(), which is one past the last valid index. Accessing bytes[right] causes an index out of bounds panic at runtime.
🧠 Conceptual
advancedWhy Use Two Pointers for Palindrome Check?
Why is the two pointer technique efficient for checking if a string is a palindrome ignoring non-alphanumeric characters and case?
Attempts:
2 left
💡 Hint
Think about how two pointers move towards each other and skip non-alphanumeric characters.
✗ Incorrect
Two pointers start at the beginning and end of the string and move inward, skipping non-alphanumeric characters. This allows checking all relevant characters in one pass efficiently.
🚀 Application
expertFinal State of Pointers After Palindrome Check
Given the Rust code below checking palindrome with two pointers, what are the final values of left and right pointers after the function returns true for input "No lemon, no melon"?
DSA Rust
fn is_palindrome(s: &str) -> (bool, usize, usize) { let s = s.to_lowercase(); let bytes = s.as_bytes(); let (mut left, mut right) = (0, bytes.len() - 1); while left < right { if !bytes[left].is_ascii_alphanumeric() { left += 1; continue; } if !bytes[right].is_ascii_alphanumeric() { right -= 1; continue; } if bytes[left] != bytes[right] { return (false, left, right); } left += 1; right -= 1; } (true, left, right) } fn main() { let test_str = "No lemon, no melon"; let (result, left, right) = is_palindrome(test_str); println!("{} {} {}", result, left, right); }
Attempts:
2 left
💡 Hint
Trace the pointers moving inward and stopping when left >= right.
✗ Incorrect
The pointers move inward skipping spaces and commas. They cross when left is 9 and right is 9, meaning the loop ends and the function returns true with these pointer values.
