InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Math & Geometry

Palindrome Number

easy Original ↗ 00:00

Problem

Given an integer x, return True if x reads the same forwards and backwards, and False otherwise. A negative number is never a palindrome because the leading minus sign has no trailing counterpart. Aim to solve it without converting the number to a string.

Examples

  • x = 121True — reversing the digits gives 121.
  • x = -121False — reversed it reads 121-, which differs from -121.
  • x = 10False — reversed it reads 01, i.e. 1, which differs from 10.

Constraints

  • -2^31 <= x <= 2^31 - 1

Think about it first

Hint 1 Negatives are out immediately. Also, any positive number ending in `0` (except `0` itself) cannot be a palindrome, because a palindrome cannot start with `0`.
Hint 2 You can rebuild the number reversed using arithmetic only: repeatedly peel the last digit with `% 10` and append it to a running reversed value with `rev = rev * 10 + digit`.
Hint 3 You don't have to reverse the whole number — reverse only the second half and compare it to the first half. Stop when the remaining "first half" is no larger than the reversed "second half." For odd digit counts, drop the middle digit with `rev // 10`.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug