Palindrome Number
Java coding interview problem for Basic Number Programs: Palindrome Number.
Checking whether a number is a Palindrome Number is one of the most common Java coding interview questions. It tests your understanding of arithmetic operations, loops, conditional statements, and logical thinking.
What is a Palindrome Number?
A Palindrome Number is a number that reads the same from left to right and from right to left.
Examples:
121
Forward
121
Backward
121
Both are the same.
Therefore,
121 is a Palindrome Number
Examples
| Number | Reverse | Palindrome? |
|---|---|---|
| 121 | 121 | ✅ Yes |
| 1221 | 1221 | ✅ Yes |
| 1331 | 1331 | ✅ Yes |
| 123 | 321 | ❌ No |
| 456 | 654 | ❌ No |
| 100 | 001 | ❌ No |
Real Interview Question
Write a Java program to determine whether a given number is a Palindrome Number.
Example 1
Input
121
Output
121 is a Palindrome Number
Example 2
Input
123
Output
123 is NOT a Palindrome Number
Understanding the Logic
To determine whether a number is a palindrome,
- Reverse the given number.
- Compare the reversed number with the original number.
- If both are equal, it is a palindrome.
- Otherwise, it is not.
Example
Original Number
121
Reverse
121
Since both are equal,
Palindrome Number
Brute Force Approach
The simplest approach is
- Reverse the number digit by digit.
- Store the reversed number.
- Compare it with the original number.
Algorithm
Step 1
Read the input number.
Step 2
Store the original number.
Step 3
Initialize
reverse = 0;
Step 4
Repeat until the number becomes zero.
Step 5
Extract the last digit.
digit = number % 10;
Step 6
Append the digit to the reversed number.
reverse = reverse * 10 + digit;
Step 7
Remove the last digit.
number = number / 10;
Step 8
Compare
original == reverse
If true,
Palindrome
Otherwise,
Not Palindrome
Dry Run
Input
121
| Iteration | Number | Digit | Reverse |
|---|---|---|---|
| 1 | 121 | 1 | 1 |
| 2 | 12 | 2 | 12 |
| 3 | 1 | 1 | 121 |
Original
121
Reverse
121
Output
Palindrome Number
Dry Run (Non-Palindrome)
Input
123
| Iteration | Number | Digit | Reverse |
|---|---|---|---|
| 1 | 123 | 3 | 3 |
| 2 | 12 | 2 | 32 |
| 3 | 1 | 1 | 321 |
Original
123
Reverse
321
Output
Not a Palindrome Number
Approach 1 — Reverse the Number
Complete Java Program
public class PalindromeNumber {
public static void main(String[] args) {
int number = 121;
int original = number;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
if (original == reverse) {
System.out.println(original + " is a Palindrome Number");
} else {
System.out.println(original + " is NOT a Palindrome Number");
}
}
}
Output
121 is a Palindrome Number
Step-by-Step Code Explanation
Step 1
Read the input number.
int number = 121;
Step 2
Store the original value.
int original = number;
We need the original number because the variable
number
will change while reversing.
Step 3
Initialize the reverse.
int reverse = 0;
Step 4
Extract the last digit.
digit = number % 10;
Example
121 % 10 = 1
Step 5
Build the reversed number.
reverse = reverse * 10 + digit;
Initially
reverse = 0
After first iteration
1
After second iteration
12
After third iteration
121
Step 6
Remove the last digit.
number = number / 10;
Example
121 / 10 = 12
12 / 10 = 1
1 / 10 = 0
Step 7
Compare both numbers.
if (original == reverse)
If equal,
Palindrome Number
Else,
Not a Palindrome Number
Why Does This Work?
Each iteration removes the last digit from the original number and appends it to the reversed number.
Eventually,
Original Number
121
becomes
Reverse Number
121
Since both are equal,
Palindrome Number
Drawbacks of This Approach
Although this solution is simple and commonly used in interviews, it creates an entirely new reversed number before performing the comparison.
For very large numbers, additional care is needed to avoid integer overflow. In the next part, we'll look at a reusable method-based implementation, discuss alternative approaches, analyze time and space complexity, cover common interview mistakes, and explore follow-up interview questions.
Approach 2 — Using a Reusable Method
Instead of writing the logic inside the main() method, create a reusable method.
This approach improves
- Code reusability
- Readability
- Testing
- Maintainability
Java Solution
public class PalindromeNumberMethod {
static boolean isPalindrome(int number) {
int original = number;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
return original == reverse;
}
public static void main(String[] args) {
int number = 1331;
if (isPalindrome(number)) {
System.out.println(number + " is a Palindrome Number");
} else {
System.out.println(number + " is NOT a Palindrome Number");
}
}
}
Output
1331 is a Palindrome Number
Approach 3 — Using String
Convert the number into a String and compare it with its reverse.
Although simple, interviewers generally expect the arithmetic solution.
Java Solution
public class PalindromeUsingString {
public static void main(String[] args) {
int number = 1221;
String original = String.valueOf(number);
String reversed = new StringBuilder(original)
.reverse()
.toString();
if (original.equals(reversed)) {
System.out.println(number + " is a Palindrome Number");
} else {
System.out.println(number + " is NOT a Palindrome Number");
}
}
}
Output
1221 is a Palindrome Number
Time Complexity
Arithmetic Approach
| Operation | Complexity |
|---|---|
| Time | O(log₁₀ n) |
| Space | O(1) |
String Approach
| Operation | Complexity |
|---|---|
| Time | O(log₁₀ n) |
| Space | O(log₁₀ n) |
Comparison
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Reverse Number | O(log n) | O(1) | ✅ Best |
| Reusable Method | O(log n) | O(1) | Good |
| StringBuilder | O(log n) | O(log n) | Easy but less preferred |
Common Mistakes
Mistake 1
Not preserving the original number.
Wrong
number = number / 10;
After the loop,
number
becomes
0
Always save it first.
int original = number;
Mistake 2
Forgetting to multiply by 10.
Wrong
reverse = reverse + digit;
Correct
reverse = reverse * 10 + digit;
Mistake 3
Using
if(number == reverse)
after modifying
number
The comparison always fails because
number
becomes
0
Mistake 4
Ignoring negative numbers.
Example
-121
Reverse
121-
Negative numbers are generally not considered palindrome numbers.
Mistake 5
Ignoring integer overflow.
Very large integers may overflow while building the reversed number.
For extremely large values,
consider using
long
or
BigInteger
Interview Follow-up Questions
Q1. Check whether a String is a Palindrome.
Q2. Check whether a Number is a Palindrome without using extra space.
Q3. Reverse an Integer.
Q4. Find the Largest Palindrome Number in an array.
Q5. Check whether a Sentence is a Palindrome.
Q6. Ignore spaces and special characters while checking palindrome.
Q7. Check whether a Linked List is a Palindrome.
Q8. Find the Next Palindrome Number.
Q9. Determine whether a Binary Number is a Palindrome.
Q10. Find all Palindrome Numbers in a given range.
Related Coding Problems
- Reverse Integer
- Reverse String
- Prime Number
- Armstrong Number
- Fibonacci Series
- Strong Number
- Anagram String
- Longest Palindromic Substring
Key Takeaways
- A Palindrome Number reads the same from left to right and right to left.
- The arithmetic solution reverses the digits using
%and/. - Store the original number before reversing it.
- The arithmetic approach uses O(log n) time and O(1) space.
- The String approach is easier to write but uses additional memory.
- The arithmetic approach is the one most commonly expected in Java coding interviews.
Interview Tip
If an interviewer asks:
"Write a Java program to check whether a number is a palindrome."
Start with the arithmetic approach using modulus (%) and division (/) operations. Explain how each digit is extracted and appended to build the reversed number. Mention the StringBuilder approach as an alternative, but emphasize that the arithmetic solution is preferred because it uses constant space (O(1)) and demonstrates a stronger understanding of number manipulation.