Hint: Start left at 0 and right at length minus one [OK]
Common Mistakes:
Using s.len() as right index causes out-of-bounds
Not making pointers mutable
Starting left pointer at 1 skips first character
3. What will be the output of the following Rust code snippet?
fn is_palindrome(s: &str) -> bool {
let bytes = s.as_bytes();
let (mut left, mut right) = (0, bytes.len() - 1);
while left < right {
if bytes[left] != bytes[right] {
return false;
}
left += 1;
right -= 1;
}
true
}
fn main() {
println!("{}", is_palindrome("racecar"));
println!("{}", is_palindrome("hello"));
}
medium
A. false\nfalse
B. false\ntrue
C. true\nfalse
D. true\ntrue
Solution
Step 1: Check palindrome for "racecar"
Characters match from both ends moving inward, so returns true.
Step 2: Check palindrome for "hello"
Characters differ at first comparison ('h' vs 'o'), so returns false.
Final Answer:
true\nfalse -> Option C
Quick Check:
racecar = true, hello = false [OK]
Hint: Check first and last chars; mismatch means false [OK]
Common Mistakes:
Assuming hello is palindrome
Confusing output order
Ignoring early return on mismatch
4. Identify the bug in this Rust function that checks palindrome and select the fix:
fn is_palindrome(s: &str) -> bool {
let bytes = s.as_bytes();
let (mut left, mut right) = (0, bytes.len());
while left < right {
if bytes[left] != bytes[right] {
return false;
}
left += 1;
right -= 1;
}
true
}
medium
A. Change right initialization to bytes.len() - 1
B. Change while condition to left <= right
C. Make left and right immutable
D. Use s.chars() instead of bytes
Solution
Step 1: Identify out-of-bounds error
Right pointer is set to bytes.len(), which is out of valid index range (0 to len-1).
Step 2: Fix pointer initialization
Set right to bytes.len() - 1 to point to last valid character index.
Final Answer:
Change right initialization to bytes.len() - 1 -> Option A
Quick Check:
Right pointer must be last index = Change right initialization to bytes.len() - 1 [OK]
Hint: Right pointer = length minus one to avoid out-of-bounds [OK]
Common Mistakes:
Using length as index causes panic
Changing while condition without fixing index
Making pointers immutable prevents movement
5. Given the string s = "A man, a plan, a canal: Panama", which Rust code modification correctly checks if s is a palindrome ignoring non-alphanumeric characters and case using two pointers?
hard
A. Convert string to uppercase and compare characters including punctuation
B. Reverse the string and compare directly without filtering
C. Count characters ignoring case but include spaces and punctuation
D. Use two pointers skipping non-alphanumeric chars and compare lowercase characters
Solution
Step 1: Understand problem requirements
We must ignore non-alphanumeric characters and case differences when checking palindrome.
Step 2: Apply two-pointer with skipping and case normalization
Move pointers inward, skip chars that are not letters or digits, compare lowercase versions of characters.
Final Answer:
Use two pointers skipping non-alphanumeric chars and compare lowercase characters -> Option D
Quick Check:
Skip non-alphanumeric + lowercase compare = Use two pointers skipping non-alphanumeric chars and compare lowercase characters [OK]
Hint: Skip non-letters/digits and compare lowercase chars [OK]
Common Mistakes:
Comparing with punctuation included
Ignoring case differences
Reversing string without filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.