PPractick

DSA interview question

Palindrome Number

Easy · Algorithms · Topics: basic maths, number theory

The problem

A ticketing system gives "lucky" status to any booking reference that reads the same forwards and backwards, like 12321 or 4554. References are stored as integers, and converting each one to a string just to check it creates millions of throwaway strings a day. Your task: decide whether an integer is a palindrome using arithmetic only. The elegant solution reverses just the second half of the number and compares it with the first half, which also avoids any risk of overflow.

Example: 121 → true, -121 → false (it reads 121- backwards), 10 → false.

Function signature

bool isPalindrome(int x);  // true if x reads the same forwards and backwards

Test cases

Input xOutputExplanation
121true
-121falseNegative numbers have a leading '-' that no digit can match
10falseEnds in 0 but doesn't start with 0
0trueA single digit is a palindrome
12321trueOdd number of digits; the middle digit is ignored
1221trueEven number of digits
123false
2147447412trueClose to INT_MAX; reversing only half never overflows

(Confirm: x is a 32-bit signed integer; solve without converting to a string.)

How interviewers phrase it

Clarifying questions to ask

  1. Are negative numbers palindromes? (Usually no.)
  2. Can I convert to a string, or should I use arithmetic?
  3. What's the input range? (Reversing the full number could overflow.)

Follow-ups to expect

  1. "Why not reverse the whole number?"
  2. "How do you know when you've reached the middle?"
  3. "How do you handle an odd number of digits?"
  4. "Why reject numbers ending in 0 upfront?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in