Is an integer a palindrome without turning it into a string? Peel digits off the end to rebuild the back half, and stop once it meets the front.
▼The problem
LeetCode 9 (Easy). Given an integer x, return true if it is a palindrome (its digits read the same forwards and backwards), false otherwise. Follow-up: solve it without converting the integer to a string.
LeetCode's examples, shown in scene 2: 121 → true; -121 → false (backwards it reads 121-); 10 → false (backwards it reads 01).
The naive way shown: str(x) == str(x)[::-1] builds a string (one extra character per digit, O(d) extra space), which the follow-up forbids; reversing the whole number with arithmetic can overflow a 32-bit integer (2147483647, the 32-bit maximum, reversed is 7463847412). The solution (O(log₁₀ n) time, O(1) space): negatives and numbers ending in 0 (other than 0 itself) are false; otherwise peel digits off the end with x % 10 and build the reversed back half (rev = rev * 10 + x % 10, x //= 10) while x > rev; then it is a palindrome if x == rev (even length) or x == rev // 10 (odd length: the middle digit sits at the end of rev). The exact code on screen (CODE in src/scenes/S5.jsx):
def is_palindrome(x):
if x < 0 or (x % 10 == 0 and x != 0):
return False
rev = 0
while x > rev:
rev = rev * 10 + x % 10
x //= 10
return x == rev or x == rev // 10Checked against str(x) == str(x)[::-1] for every x from -2,000 to 199,999, on 300,000 random 32-bit integers and on 300,000 built palindromes (even and odd length). Traces: 1221 → (d, x, rev) (1, 122, 1), (2, 12, 12), equal; 12321 → (1, 1232, 1), (2, 123, 12), (3, 12, 123), then 123 // 10 = 12; 1231 → (1, 123, 1), (3, 12, 13), and 12 is neither 13 nor 1; 123321 takes three peels; 2147483647 stops after five peels at x = 21474, rev = 74638. Every value on screen is computed in code: runPal() in src/gumballs.jsx runs the same loop and records every peel; it is checked against the string definition on 14 numbers when the module loads, and every scene asserts the values it shows. src/gen_score.py re-runs the loop (run_pal) and asserts the traces its sound cues follow.
On "rev only holds half the digits": the loop stops as soon as rev >= x, so rev ends with about half the digits (at most one more than half; for a 10-digit 32-bit input, at most 6), far below the 32-bit limit. The scene shows the 32-bit maximum itself: rev stops at 74638, five digits.
The solution
def is_palindrome(x):
if x < 0 or (x % 10 == 0 and x != 0):
return False
rev = 0
while x > rev:
rev = rev * 10 + x % 10
x //= 10
return x == rev or x == rev // 10Transcript
Palindrome Number. Given an integer x, return true if its digits read the same forwards and backwards. The catch: do it without turning x into a string.
One twenty one reads one, two, one either way: true. Minus one twenty one, backwards, is one twenty one minus: false. Ten backwards is zero one: false again.
The easy way compares the string with its reverse. But that builds a string, one extra character per digit, and the catch rules it out. Reversing the whole number with arithmetic can overflow: two billion and change, backwards, is over seven billion.
The key idea: reverse only half. First, the easy falses. Negatives, and numbers ending in zero, except zero itself, since no palindrome starts with a zero. Then peel the last digit off with x mod ten, and push it onto a second number, rev: rev times ten, plus the digit. Keep going while x is bigger than rev. For one two two one, two peels leave twelve and twelve: a match.
In code, the easy cases return false. While x is bigger than rev, move one digit across. Then x must equal rev, or rev with its last digit dropped.
Let's run one two three two one. Peel the one: rev is one. Peel the two: rev is twelve, and x is one twenty three. Still bigger, so peel the three: rev is one twenty three, x is twelve. Rev has passed x, so stop. An odd length leaves the middle digit at the end of rev, so drop it: twelve. Twelve equals twelve: true.
Now one two three one. Two peels leave x at twelve and rev at thirteen. Twelve isn't thirteen, and it isn't one: false.
Each peel removes one digit, and we stop halfway: log n time. Just two numbers: constant space. And rev only holds half the digits, so it can't overflow.
Reject the easy falses, peel half the digits, compare. That's Palindrome Number.