Write a Java program to check whether a given string or number is a palindrome.
A palindrome is a string or number that reads the same forwards and backwards, such as "madam" or 12321. For strings, the standard solution is the two-pointer technique: compare the first and last characters and move both pointers inward; any mismatch proves it is not a palindrome. For numbers, reverse the digits arithmetically with the modulus (%) and division (/) operators and compare the reversed value with the original.
Interviewers use this question to test loops, conditionals, and String handling — the same skills SDETs apply daily when validating UI text or generating test data in Selenium/TestNG frameworks.
Two-pointer string check — compare
charAt(left)withcharAt(right)whileleft < right; runs in O(n) time with O(1) extra space.Number reversal — repeat
reversed = reversed * 10 + number % 10andnumber /= 10until the number is exhausted; equal original and reversed values confirm a palindrome.StringBuilder shortcut —
new StringBuilder(s).reverse().toString().equals(s)works in one line, but the manual loop shows stronger fundamentals.Normalization — call
toLowerCase()so"Madam"passes, and strip spaces or punctuation when checking full phrases.Edge cases — negative numbers are conventionally not palindromes, while empty and single-character strings are palindromes by definition.
Key terms: palindrome, two-pointer technique,
charAt(), modulus operator, digit reversal,StringBuilder.reverse(), time complexity
public class PalindromeCheck {
// Check a string palindrome using the two-pointer technique
static boolean isPalindromeString(String input) {
String s = input.toLowerCase();
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
// Check a number palindrome by reversing its digits
static boolean isPalindromeNumber(int number) {
if (number < 0) return false; // negatives are not palindromes
int original = number, reversed = 0;
while (number > 0) {
reversed = reversed * 10 + number % 10;
number /= 10;
}
return original == reversed;
}
public static void main(String[] args) {
System.out.println(isPalindromeString("Madam")); // Output: true
System.out.println(isPalindromeString("Selenium")); // Output: false
System.out.println(isPalindromeNumber(12321)); // Output: true
System.out.println(isPalindromeNumber(12345)); // Output: false
}
}The program demonstrates both palindrome checks. isPalindromeString lowercases the input so the check is case-insensitive, then walks two pointers from the ends toward the middle, returning false on the first mismatched pair — "Madam" passes while "Selenium" fails. isPalindromeNumber peels off the last digit with number % 10, appends it to reversed by multiplying by 10, and shrinks the number with integer division; when the loop ends, 12321 equals its reversal while 12345 does not. Both methods run in O(n) time with constant extra space, which is why this pair of solutions is the expected interview answer.
Show both variants (string and number) and state the complexity up front: O(n) time, O(1) space for the two-pointer approach. Expect follow-ups like "solve it without StringBuilder", "ignore spaces and punctuation in a phrase like A man, a plan, a canal: Panama", "handle negative numbers or integer overflow when reversing a large int", or "do it recursively".
The classic mistake is offering only the StringBuilder one-liner — interviewers usually push back and ask for the manual loop, so lead with the loop and mention the one-liner as an alternative.