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 the following Java code that checks if a string is a palindrome ignoring non-alphanumeric characters and case?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) return false; left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("A man, a plan, a canal: Panama")); } }
Attempts:
2 left
💡 Hint
Focus on how non-alphanumeric characters are skipped and case is ignored.
✗ Incorrect
The code uses two pointers moving inward, skipping non-alphanumeric characters and comparing characters case-insensitively. The input string is a well-known palindrome phrase ignoring punctuation and case, so the output is true.
❓ Predict Output
intermediateResult of Palindrome Check on Mixed Characters
What will be the output of this Java method call?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) return false; left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("No lemon, no melon!")); } }
Attempts:
2 left
💡 Hint
Check if the string reads the same forwards and backwards ignoring punctuation and case.
✗ Incorrect
The string "No lemon, no melon!" is a palindrome ignoring punctuation and case, so the method returns true.
❓ Predict Output
advancedOutput for Non-Palindrome String
What does the following Java code print when checking if "Hello, World!" is a palindrome?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) return false; left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("Hello, World!")); } }
Attempts:
2 left
💡 Hint
Check if the characters match when ignoring punctuation and case.
✗ Incorrect
The string "Hello, World!" is not a palindrome ignoring punctuation and case, so the method returns false.
❓ Predict Output
advancedOutput of Palindrome Check on Empty String
What will the following Java code print when checking if an empty string is a palindrome?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) return false; left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("")); } }
Attempts:
2 left
💡 Hint
An empty string is considered a palindrome by definition.
✗ Incorrect
Since the string is empty, the method returns true as there are no characters to contradict palindrome property.
🧠 Conceptual
expertWhy Two Pointers Work for Palindrome Checking
Why is the two-pointer technique effective for checking if a string is a palindrome ignoring non-alphanumeric characters and case?
Attempts:
2 left
💡 Hint
Think about how palindrome property relates to symmetry from both ends.
✗ Incorrect
The two-pointer approach efficiently compares characters from the start and end, moving inward, skipping non-alphanumeric characters, and ignoring case, which directly tests the palindrome property.
