Bird
Raised Fist0
DSA Javaprogramming~20 mins

Valid Palindrome Two Pointer in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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"));
    }
}
ACompilation error
Bfalse
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
Focus on how non-alphanumeric characters are skipped and case is ignored.
❓ Predict Output
intermediate
2:00remaining
Result 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!"));
    }
}
AIndexOutOfBoundsException
Bfalse
CNullPointerException
Dtrue
Attempts:
2 left
💡 Hint
Check if the string reads the same forwards and backwards ignoring punctuation and case.
❓ Predict Output
advanced
2:00remaining
Output 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!"));
    }
}
Afalse
BCompilation error
CStringIndexOutOfBoundsException
Dtrue
Attempts:
2 left
💡 Hint
Check if the characters match when ignoring punctuation and case.
❓ Predict Output
advanced
2:00remaining
Output 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(""));
    }
}
Atrue
Bfalse
CStringIndexOutOfBoundsException
DNullPointerException
Attempts:
2 left
💡 Hint
An empty string is considered a palindrome by definition.
🧠 Conceptual
expert
2:00remaining
Why 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?
ABecause it uses recursion to check each character against all others in the string.
BBecause it compares characters from both ends moving inward, skipping irrelevant characters, ensuring all pairs match.
CBecause it sorts the string first and then compares characters sequentially.
DBecause it converts the string to uppercase and checks only the first half.
Attempts:
2 left
💡 Hint
Think about how palindrome property relates to symmetry from both ends.

Practice

(1/5)
1. What is the main idea behind the two-pointer technique to check if a string is a palindrome?
easy
A. Sort the characters and check if the sorted string equals the original
B. Use two pointers starting from both ends and move towards the center comparing characters
C. Count the frequency of each character and check if all are even
D. Reverse the string and compare it with the original string

Solution

  1. Step 1: Understand the two-pointer approach

    Two pointers start at the beginning and end of the string and move inward.
  2. Step 2: Compare characters at pointers

    If characters at both pointers match, move pointers closer; if not, string is not palindrome.
  3. Final Answer:

    Use two pointers starting from both ends and move towards the center comparing characters -> Option B
  4. Quick Check:

    Two-pointer technique = Compare ends inward [OK]
Hint: Compare characters from both ends moving inward [OK]
Common Mistakes:
  • Trying to reverse the string instead of using pointers
  • Checking only half the string without comparing both ends
  • Ignoring case or non-alphanumeric characters
2. Which of the following Java code snippets correctly initializes two pointers for palindrome checking on string s?
easy
A. int left = 0, right = s.length() - 1;
B. int left = 0, right = s.length();
C. int left = 1, right = s.length() - 1;
D. int left = 0, right = s.length() + 1;

Solution

  1. Step 1: Identify valid pointer initialization

    String indices start at 0 and end at length - 1, so right pointer must be s.length() - 1.
  2. Step 2: Check each option

    int left = 0, right = s.length() - 1; correctly sets left to 0 and right to s.length() - 1; others are off by one or invalid.
  3. Final Answer:

    int left = 0, right = s.length() - 1; -> Option A
  4. Quick Check:

    Right pointer = last index = length - 1 [OK]
Hint: Right pointer = string length minus one [OK]
Common Mistakes:
  • Setting right pointer to s.length() causing out-of-bounds
  • Starting left pointer at 1 instead of 0
  • Using length + 1 causing errors
3. What will be the output of the following Java code snippet?
String s = "A man, a plan, a canal: Panama";
boolean isPalindrome = true;
int left = 0, right = s.length() - 1;
while (left < right) {
  char leftChar = Character.toLowerCase(s.charAt(left));
  char rightChar = Character.toLowerCase(s.charAt(right));
  if (!Character.isLetterOrDigit(leftChar)) {
    left++;
  } else if (!Character.isLetterOrDigit(rightChar)) {
    right--;
  } else {
    if (leftChar != rightChar) {
      isPalindrome = false;
      break;
    }
    left++;
    right--;
  }
}
System.out.println(isPalindrome);
medium
A. Compilation error
B. false
C. true
D. Runtime exception

Solution

  1. Step 1: Understand the code logic

    The code skips non-alphanumeric characters and compares lowercase letters from both ends moving inward.
  2. Step 2: Trace the string "A man, a plan, a canal: Panama"

    Ignoring spaces and punctuation, the cleaned string is "amanaplanacanalpanama" which reads the same forward and backward.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Ignoring non-alphanumeric, palindrome check = true [OK]
Hint: Ignore non-letters and compare lowercase chars [OK]
Common Mistakes:
  • Not skipping non-alphanumeric characters
  • Not converting characters to lowercase
  • Breaking loop too early without full check
4. Identify the bug in this Java code snippet for palindrome checking:
String s = "racecar";
int left = 0, right = s.length() - 1;
boolean isPalindrome = true;
while (left <= right) {
  if (s.charAt(left) != s.charAt(right)) {
    isPalindrome = false;
    break;
  }
  left++;
  right--;
}
System.out.println(isPalindrome);
medium
A. The code does not skip non-alphanumeric characters
B. The pointers should start at 1 and length - 2
C. The loop condition should be left < right, not left <= right
D. The code does not convert characters to lowercase

Solution

  1. Step 1: Analyze loop condition

    Using left <= right causes the middle character to be compared with itself unnecessarily, but it does not cause wrong result here.
  2. Step 2: Check correctness of condition

    The main issue is that the code does not convert characters to lowercase, so it fails for case-insensitive palindrome checks.
  3. Final Answer:

    The code does not convert characters to lowercase -> Option D
  4. Quick Check:

    Case sensitivity causes incorrect palindrome detection [OK]
Hint: Convert characters to lowercase to handle case sensitivity [OK]
Common Mistakes:
  • Using <= causes unnecessary middle char check
  • Ignoring case sensitivity
  • Not handling non-alphanumeric characters
5. Given the string s = "No lemon, no melon", which modification to the two-pointer palindrome check is necessary to correctly identify it as a palindrome?
hard
A. Convert characters to lowercase and skip non-alphanumeric characters during comparison
B. Only compare characters without any conversion or skipping
C. Reverse the string and compare directly without pointers
D. Use a stack to store characters and compare with the original

Solution

  1. Step 1: Understand the string content

    The string contains spaces and uppercase letters which should be ignored or normalized for palindrome check.
  2. Step 2: Modify two-pointer approach

    Convert characters to lowercase and skip non-alphanumeric characters to compare only valid letters.
  3. Final Answer:

    Convert characters to lowercase and skip non-alphanumeric characters during comparison -> Option A
  4. Quick Check:

    Normalize case and skip invalid chars for correct palindrome [OK]
Hint: Normalize case and ignore spaces/punctuation [OK]
Common Mistakes:
  • Not ignoring spaces and punctuation
  • Not converting uppercase to lowercase
  • Assuming direct comparison without cleaning works
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard