Definition
- Palindromic number – an integer that reads the same from left to right as from right to left, such as
121or1221. - Digit reversal – repeatedly extract the last digit with
n % 10, drop it withn / 10, and build the reversed number asreversed = reversed * 10 + digit. - Negative numbers – never palindromes by convention because the minus sign does not mirror.
- Overflow guard – use a
longaccumulator to prevent integer overflow when reversing large numbers. - Complexity – O(log10 n) time for the digit count, O(1) space.
Rajesh at National Bank of India receives cheque reference numbers, and some are deliberately minted as palindromes. A palindromic number typed in reverse still equals itself, neutralising digit-transposition mistakes during branch reconciliation. The clerk reverses the digits and checks whether the result matches the original.
Understanding the Concepts
What is a palindromic number?
A palindromic number is an integer that reads the same forwards and backwards. Examples include 121, 1221, 0, and 9. A single-digit number is always a palindrome. Zero is a palindrome.
package com.bank.core;
public class NumberPalindrome {
public static boolean isPalindrome(int num) {
if (num < 0) return false;
long original = num;
long reversed = 0;
long n = original;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed == original;
}
public static void main(String[] args) {
System.out.println(isPalindrome(121)); // true
System.out.println(isPalindrome(1221)); // true
System.out.println(isPalindrome(123)); // false
System.out.println(isPalindrome(-121)); // false
}
}
true
true
false
false
How does digit reversal work?
The core trick is digit extraction. For any non-negative integer, n % 10 gives the last digit and n / 10 (integer division) removes it. You build the reversed number by multiplying the accumulator by 10 (to shift existing digits left) and adding the extracted digit.
For 121: first step reversed = 1, second step reversed = 12, third step reversed = 121. Compare 121 == 121, so it is a palindrome.
package com.bank.core;
public class DigitReversalSteps {
public static void main(String[] args) {
int original = 121;
long n = original;
long reversed = 0;
int step = 1;
while (n > 0) {
long digit = n % 10;
reversed = reversed * 10 + digit;
System.out.println("Step " + step + ": digit=" + digit + ", reversed=" + reversed);
n /= 10;
step++;
}
System.out.println(original + " is palindrome: " + (reversed == original));
}
}
Step 1: digit=1, reversed=1
Step 2: digit=2, reversed=12
Step 3: digit=1, reversed=121
121 is palindrome: true
Why are negative numbers not palindromes?
By convention, a negative number is never a palindrome because the minus sign at the front does not appear at the end when reversed. For example, -121 reversed would be 121-, which is not a valid number. Most problem definitions exclude negatives immediately.
package com.bank.core;
public class NegativeNotPalindrome {
public static boolean isPalindrome(int num) {
if (num < 0) return false; // immediate rejection
long original = num;
long reversed = 0;
long n = original;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed == original;
}
public static void main(String[] args) {
System.out.println(isPalindrome(-121)); // false
System.out.println(isPalindrome(-1)); // false
System.out.println(isPalindrome(0)); // true
}
}
false
false
true
How do you prevent integer overflow?
If the original number is a large int close to Integer.MAX_VALUE, the reversed number can exceed the int range and wrap around. The safe approach is to accumulate the reversed value in a long, which holds much larger values.
package com.bank.core;
public class OverflowSafe {
public static boolean isPalindrome(int num) {
if (num < 0) return false;
long original = num;
long reversed = 0; // long prevents overflow
long n = original;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed == original;
}
public static void main(String[] args) {
System.out.println(isPalindrome(2147483647)); // false (max int)
System.out.println(isPalindrome(1000000001)); // true
}
}
false
true
What is the half-reversal optimisation?
The half-reversal technique reverses only the second half of the number and compares it with the first half. You stop when the remaining first half is less than or equal to the reversed second half. This avoids overflow entirely because you never build a number larger than the original.
For even-length numbers like 1221, the two halves become 12 and 12 (equal, so palindrome). For odd-length numbers like 12321, the halves become 12 and 123, and you compare 12 with 123/10 = 12.
package com.bank.core;
public class HalfReversal {
public static boolean isPalindrome(int num) {
if (num < 0 || (num != 0 && num % 10 == 0)) return false;
int reversed = 0;
while (num > reversed) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return num == reversed || num == reversed / 10;
}
public static void main(String[] args) {
System.out.println(isPalindrome(1221)); // true (even length)
System.out.println(isPalindrome(12321)); // true (odd length)
System.out.println(isPalindrome(123)); // false
}
}
true
true
false
What about numbers ending in zero?
Numbers ending in zero are a special case. Except for zero itself, no number that ends in zero can be a palindrome because a leading zero is not valid. For example, 10 reversed is 01, which is 1, not 10. The half-reversal method guards against this explicitly.
package com.bank.core;
public class TrailingZero {
public static boolean isPalindrome(int num) {
if (num < 0 || (num != 0 && num % 10 == 0)) return false;
int reversed = 0;
while (num > reversed) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return num == reversed || num == reversed / 10;
}
public static void main(String[] args) {
System.out.println(isPalindrome(10)); // false
System.out.println(isPalindrome(100)); // false
System.out.println(isPalindrome(0)); // true
}
}
false
false
true
What is the time and space complexity?
The algorithm processes each digit once, so the time is O(d) where d is the number of digits, which is O(log10 n). The space is O(1) because only a few integer variables are used. The half-reversal technique processes only half the digits but the asymptotic complexity is the same.
What is the banking use case?
At National Bank of India, settlement reference codes are sometimes deliberately minted as palindromes. A palindromic code typed in reverse still equals itself, which neutralises a whole class of digit-transposition mistakes that clerks make during branch-to-branch reconciliation. Rajesh's validation service checks each code, and Pankaj's reconciliation job counts how many codes in the daily batch are palindromic as a data-quality metric.
package com.bank.service;
public class ReferenceCodeValidator {
public static boolean isPalindromicCode(long code) {
if (code < 0) return false;
long original = code;
long reversed = 0;
while (code > 0) {
reversed = reversed * 10 + code % 10;
code /= 10;
}
return reversed == original;
}
public static void main(String[] args) {
long[] codes = {1234321, 9876789, 1234567};
for (long c : codes) {
System.out.println(c + " palindromic: " + isPalindromicCode(c));
}
}
}
1234321 palindromic: true
9876789 palindromic: true
1234567 palindromic: false
What are common interview follow-ups?
Interviewers often extend the number palindrome problem with these variants:
- Do it without converting to a string (the digit-reversal or half-reversal shown above).
- Find the next palindromic number after a given number.
- Check if a number can become a palindrome by rearranging its digits.
- Largest palindrome product of two n-digit numbers.
package com.bank.core;
public class NextPalindrome {
public static int nextPalindrome(int n) {
int candidate = n + 1;
while (!isPalindrome(candidate)) candidate++;
return candidate;
}
private static boolean isPalindrome(int num) {
if (num < 0) return false;
long original = num;
long reversed = 0;
long x = original;
while (x > 0) { reversed = reversed * 10 + x % 10; x /= 10; }
return reversed == original;
}
public static void main(String[] args) {
System.out.println("Next palindrome after 123: " + nextPalindrome(123));
System.out.println("Next palindrome after 999: " + nextPalindrome(999));
}
}
Next palindrome after 123: 131
Next palindrome after 999: 1001
Visual Overview
Number Palindrome: Full Reversal ═════════════════════════════════ Input: 1221 Step 1: n=1221 digit=1 reversed=1 Step 2: n=122 digit=2 reversed=12 Step 3: n=12 digit=2 reversed=122 Step 4: n=1 digit=1 reversed=1221 1221 == 1221 → Palindrome! Half-Reversal Optimisation ══════════════════════════ Input: 12321 num=12321 reversed=0 num=1232 reversed=1 (num > reversed, continue) num=123 reversed=12 (num > reversed, continue) num=12 reversed=123 (num < reversed, stop) num == reversed/10 → 12 == 123/10 → 12 == 12 → Palindrome!
Key Takeaway
A palindromic number reads the same forwards and backwards, and the clean way to test it is to reverse the digits arithmetically and compare the reversed value to the original. You reverse by repeatedly extracting the last digit with modulo 10 and building the reversed number by multiplying the accumulator by 10 and adding that digit. The whole operation runs in time proportional to the number of digits, which is O(log10 n).
Two senior details matter in practice. First, overflow: for a large number near the integer maximum, the reversed value can exceed the integer range, so accumulating in a long is the safe default. Second, the half-reversal optimisation reverses only the second half and compares it to the first half, which avoids overflow entirely and does half the work.
In the banking project at National Bank of India, Rajesh validates settlement reference codes. Palindromic codes are deliberately minted because a palindrome typed in reverse still equals itself, neutralising digit-transposition mistakes during branch reconciliation. The validation service checks each code, and the reconciliation job counts palindromic codes as a data-quality metric.
Remember one sentence: reverse the digits with modulo 10 and integer division, compare to the original, and use a long accumulator or the half-reversal trick to avoid overflow.
---