9. Palindrome Number
easyAsked at HPHP firmware and driver code handles numeric identifiers for device serial numbers, cartridge IDs, and page counters. Palindrome Number tests digit-manipulation skills without string conversion — a meaningful constraint in embedded C where string allocation is expensive.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in HP loops.
- Glassdoor (2025-Q3)— Cited in HP entry-level SWE and intern phone-screen reports as a math/digit-manipulation warm-up.
- Blind (2025-08)— HP candidates mention Palindrome Number as a simple follow-up to Two Sum in first-round screens.
Problem
Given an integer x, return true if x is a palindrome, and false otherwise. A palindrome reads the same forward and backward.
Constraints
−2^31 <= x <= 2^31 − 1
Examples
Example 1
x = 121trueExplanation: 121 reads as 121 from left to right and from right to left.
Example 2
x = -121falseExplanation: From left to right it reads -121; from right to left 121-. Therefore it is not a palindrome.
Example 3
x = 10falseExplanation: Reads 01 from right to left — not equal to 10.
Approaches
1. String conversion
Convert x to a string, reverse it, and compare. Simple but allocates a string.
- Time
- O(log x)
- Space
- O(log x)
function isPalindrome(x) {
if (x < 0) return false;
const s = x.toString();
return s === s.split('').reverse().join('');
}Tradeoff: Easy to implement but violates the follow-up constraint of no string conversion. Mention it first, then optimize.
2. Reverse half the digits (no string conversion)
Reverse only the second half of the number's digits and compare to the first half. Handles odd-length numbers by dividing out the middle digit.
- Time
- O(log x)
- Space
- O(1)
function isPalindrome(x) {
// Negatives and numbers ending in 0 (except 0 itself) are never palindromes
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + (x % 10);
x = Math.floor(x / 10);
}
// Even length: x === reversedHalf
// Odd length: x === Math.floor(reversedHalf / 10) (discard middle digit)
return x === reversedHalf || x === Math.floor(reversedHalf / 10);
}Tradeoff: O(1) space, no string allocation. The while-loop terminates at the midpoint, avoiding integer overflow of the full reversal. This is the answer HP expects when asking 'can you do it without converting to a string?'
HP-specific tips
HP interviewers frequently add the follow-up 'solve it without converting to a string' upfront — go straight to the half-reversal approach. Explain the termination condition clearly: 'I stop when x ≤ reversedHalf, meaning I've processed at least half the digits.' The edge-case for numbers ending in 0 (other than 0 itself) is easy to miss — catch it with an early return.
Common mistakes
- Not handling negative numbers — all negatives fail immediately.
- Reversing the entire number — risks integer overflow for large inputs; reversing only half avoids this.
- Forgetting x = 0 is a valid palindrome — the early-return condition for multiples of 10 must explicitly allow x === 0.
- Incorrect termination: stopping when x < reversedHalf instead of x <= reversedHalf misses even-length numbers.
Follow-up questions
An interviewer at HP may pivot to one of these next:
- Is there a way to solve this in O(1) space without any digit reversal?
- How would you check if a large number (stored as an array of digits) is a palindrome?
- Extend to detect palindromic numbers in an arbitrary base (not base 10).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why only reverse half the digits?
Reversing all digits of a large integer can overflow. Reversing half is sufficient because we compare the two halves against each other — no full reversal needed.
How do you handle odd-length numbers?
After the loop, reversedHalf has one extra digit (the middle). Divide reversedHalf by 10 to discard it, then compare with x.
Why is a number ending in 0 (except 0 itself) not a palindrome?
For the number to be a palindrome, the first digit must match the last. But the first digit of any positive integer is never 0. So trailing zeros guarantee non-palindrome.
Practice these live with InterviewChamp.AI
Drill Palindrome Number and other HP interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →