Decide whether an integer reads the same forwards and backwards without converting it to a string. Reverse only the second half of the digits and compare it to the first half — half the work, and no overflow to worry about.
Return true if integer x is a palindrome — it reads the same left-to-right and right-to-left. Examples: x = 121 → true; x = -121 → false (the leading minus has no trailing match); x = 10 → false (reversed it would be 01). The constraint: solve it without turning the number into a string.
x into a growing reverted, while x shrinks from the front. The moment x ≤ reverted you have crossed the midpoint: reverted holds the reversed second half and x holds the remaining first half. A palindrome iff those two halves match.x < 0it can't be a palindrome (the minus sign). If x % 10 === 0 and x !== 0, it ends in 0but doesn't start with one → reject.x > reverted, push the last digit of x onto reverted = reverted * 10 + x % 10, then drop it with x = Math.trunc(x / 10). Stopping at x ≤ revertedmeans we only ever process half the digits — that's why there is no overflow concern.x === reverted.reverted; discard it with Math.trunc(reverted / 10) and compare → palindrome iff x === Math.trunc(reverted / 10).10 slip through, or they reverse the whole number and risk overflow. Reversing only half sidesteps overflow entirely, but then you must remember to drop the middle digit for odd-length inputs with reverted / 10.Both are O(d) in time where d is the digit count (≤ 10 for a 32-bit int), so effectively constant. The half-reversal wins on space (O(1)) and respects the no-string constraint, and because it never builds the full reversed value it can't overflow.
% 10, push with * 10 + d” idiom is exactly Reverse Integer (LC 7). Stopping at a midpoint mirrors the two-pointer meet-in-the-middle used for string/array palindromes, and the digit-by-digit processing reappears in Add Strings and Plus One.121. We will reverse only the second half of the digits and compare it to the first half — no string conversion, no overflow risk.1▶function isPalindrome(x: number): boolean {2▶ // Negatives and any number ending in 0 (except 0 itself) are never palindromes.3▶ if (x < 0 || (x % 10 === 0 && x !== 0)) return false;45 let reverted = 0;6 while (x > reverted) {7 reverted = reverted * 10 + (x % 10); // push last digit of x onto reverted8 x = Math.trunc(x / 10); // drop that digit from x9 }1011 // Even length: x === reverted. Odd length: drop the middle digit (reverted / 10).12 return x === reverted || x === Math.trunc(reverted / 10);13}
function isPalindrome(x: number): boolean {
// Negatives and any number ending in 0 (except 0 itself) are never palindromes.
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reverted = 0;
while (x > reverted) {
reverted = reverted * 10 + (x % 10); // push last digit of x onto reverted
x = Math.trunc(x / 10); // drop that digit from x
}
// Even length: x === reverted. Odd length: drop the middle digit (reverted / 10).
return x === reverted || x === Math.trunc(reverted / 10);
}-with no trailing counterpart, so it's never a palindrome. A number ending in 0 (other than 0 itself) would need to start with 0when reversed, which integers don't — reject both up front.reverted starts at 0. The condition x > reverted keeps peeling digits off the back of x until reverted has caught up to (or passed) x — the midpoint of the number.reverted = reverted * 10 + (x % 10) pushes x's last digit onto reverted, then x = Math.trunc(x / 10) drops it. Math.trunc (not Math.floor) truncates toward zero, though after the early reject x is always non-negative here.x === reverted. For an odd count the middle digit ended up inside reverted; Math.trunc(reverted / 10) chops it off so we can compare the remaining halves. Either match means palindrome.s === [...s].reverse().join('')or a two-pointer scan from both ends. It's O(d) space and the problem explicitly asks you to avoid it.x === reverted. Odd: the middle digit sits in reverted, so compare against Math.trunc(reverted / 10).0 → true; -121 → false; 10 → false via the trailing-zero check.function isPalindrome(x: number): boolean {
// Negatives and any number ending in 0 (except 0 itself) are never palindromes.
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reverted = 0;
while (x > reverted) {
reverted = reverted * 10 + (x % 10); // push last digit of x onto reverted
x = Math.trunc(x / 10); // drop that digit from x
}
// Even length: x === reverted. Odd length: drop the middle digit (reverted / 10).
return x === reverted || x === Math.trunc(reverted / 10);
}The straightforward approach the prompt asks you to avoid: render the number as a string and compare characters inward from both ends.
function isPalindrome(x: number): boolean {
if (x < 0) return false;
const s = String(x);
let i = 0;
let j = s.length - 1;
while (i < j) {
if (s[i] !== s[j]) return false;
i++;
j--;
}
return true;
}O(1) space with no overflow risk.| palindrome integer without a string | reverse only the second half |
| when to stop peeling digits | loop while x > reverted (midpoint) |
| odd number of digits | compare x to Math.trunc(reverted / 10) |
| instant false cases | x < 0, or x % 10 === 0 && x !== 0 |
function isPalindrome(x: number): boolean {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reverted = 0;
while (x > reverted) {
reverted = reverted * 10 + (x % 10);
x = Math.trunc(x / 10);
}
return x === reverted || x === Math.trunc(reverted / 10);
}x < 0) and any non-zero number ending in 0 (x % 10 === 0 && x !== 0).x = -121 not a palindrome?x = 10 return false?x = 121. What are x and reverted when the loop stops?