A palindrome is a word, phrase, number, or sequence that reads the same backward as forward, ignoring spaces, punctuation, and capitalization.
Click to reveal answer
beginner
How can you check if a string is a palindrome using two pointers?
Use two pointers: one starting at the beginning and one at the end of the string. Move both pointers towards the center, comparing characters. If all pairs match, the string is a palindrome.
Click to reveal answer
intermediate
Why do we ignore case and non-alphanumeric characters when detecting palindromes?
Ignoring case and non-alphanumeric characters helps detect palindromes in phrases or sentences where spaces, punctuation, or letter case do not affect the palindrome property.
Click to reveal answer
intermediate
What is the time complexity of checking a palindrome using the two-pointer technique?
The time complexity is O(n), where n is the length of the string, because each character is checked at most once.
Click to reveal answer
beginner
Show a simple Java method signature for palindrome detection.
public static boolean isPalindrome(String s) { ... }
Click to reveal answer
Which of the following strings is a palindrome?
Aracecar
Bhello
Cworld
Djava
✗ Incorrect
racecar reads the same backward and forward.
What technique uses two pointers to check palindrome?
ADivide and conquer
BTwo-pointer technique
CDynamic programming
DGreedy algorithm
✗ Incorrect
Two-pointer technique compares characters from both ends moving towards the center.
When checking palindromes, why do we often remove spaces and punctuation?
ATo simplify the string and focus on letters and digits
BTo increase string length
CTo make the string uppercase
DTo sort the characters
✗ Incorrect
Removing spaces and punctuation helps detect palindromes in phrases ignoring non-alphanumeric characters.
What is the time complexity of palindrome detection using two pointers?
AO(log n)
BO(n^2)
CO(1)
DO(n)
✗ Incorrect
Each character is checked once, so time complexity is linear.
Which Java method signature correctly represents a palindrome check?
Aprivate void checkPalindrome()
Bpublic int palindromeCheck(String s)
Cpublic static boolean isPalindrome(String s)
Dpublic String palindrome(String s)
✗ Incorrect
The method returns a boolean indicating if the string is a palindrome.
Explain how the two-pointer technique works for palindrome detection.
Think about checking characters from both ends moving inward.
You got /5 concepts.
Describe why preprocessing a string (like removing spaces and converting to lowercase) is important in palindrome detection.
Consider how phrases with spaces and punctuation can still be palindromes.
You got /4 concepts.
Practice
(1/5)
1. What does a palindrome string mean in the context of palindrome detection?
easy
A. A string with all unique characters
B. A string that contains only vowels
C. A string that is sorted alphabetically
D. A string that reads the same forwards and backwards
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same forwards and backwards.
Step 2: Compare options with definition
Only "a string that reads the same forwards and backwards" matches this definition exactly.
Final Answer:
A string that reads the same forwards and backwards -> Option D
Quick Check:
Palindrome = Same forwards and backwards [OK]
Hint: Palindrome means mirror reading same both ways [OK]
Common Mistakes:
Confusing palindrome with unique characters
Thinking palindrome means sorted string
Assuming palindrome only has vowels
2. Which of the following Java code snippets correctly checks if a string s is a palindrome?
easy
A. for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; } return true;
B. for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(i + 1)) return false; } return true;
C. for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) == s.charAt(i + 1)) return false; } return true;
D. for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(s.length() - i)) return false; } return true;
Solution
Step 1: Check loop and character comparison logic
for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; } return true; compares characters from start and end moving towards center correctly.
Step 2: Identify errors in other options
Adjacent chars comparison is incorrect; equality check instead of inequality is wrong; off-by-one index error.
Final Answer:
for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; } return true; -> Option A
Quick Check:
Compare chars from ends moving inward [OK]
Hint: Compare chars from start and end moving inward [OK]
Common Mistakes:
Using wrong indices causing index out of bounds
Comparing adjacent characters instead of mirrored ones
Incorrect loop range causing incomplete checks
3. What will be the output of the following Java code?
String s = "racecar";
boolean isPalindrome = true;
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
System.out.println(isPalindrome);
medium
A. false
B. Compilation error
C. true
D. Runtime exception
Solution
Step 1: Analyze the string and loop logic
String "racecar" is a palindrome. Loop compares mirrored characters correctly.
Step 2: Check if any mismatch occurs
No mismatch found, so isPalindrome remains true and prints true.
Final Answer:
true -> Option C
Quick Check:
"racecar" reads same forwards and backwards [OK]
Hint: Check mirrored chars; no mismatch means true [OK]
Common Mistakes:
Assuming palindrome check fails for odd length strings
Confusing loop range causing missed checks
Misreading output as string instead of boolean
4. Identify the error in the following Java code that attempts to check if a string str is a palindrome:
boolean checkPalindrome(String str) {
for (int i = 0; i <= str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - i)) {
return false;
}
}
return true;
}
medium
A. The loop condition should be i < str.length() / 2 instead of <=.
B. The index str.length() - i causes an index out of bounds error.
C. The method should return void instead of boolean.
D. The comparison operator should be == instead of !=.
Solution
Step 1: Check index usage in charAt
Index str.length() - i is off by one; last valid index is str.length() - 1.
Step 2: Identify error cause
Accessing str.charAt(str.length() - i) when i=0 causes index out of bounds.
Final Answer:
Index str.length() - i causes out of bounds error -> Option B
Quick Check:
String indices start at 0, max index = length-1 [OK]
Hint: Check if indices go beyond string length - 1 [OK]
Common Mistakes:
Using <= in loop causing extra iteration
Confusing comparison operators
Wrong return type for boolean method
5. Given a list of strings ["level", "world", "deed", "java", "noon"], which Java code snippet correctly returns a new list containing only the palindromes?
hard
A. List<String> palindromes = list.stream().filter(s -> new StringBuilder(s).reverse().toString().equals(s)).collect(Collectors.toList());