DSA interview question
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.
bool isPalindrome(int x); // true if x reads the same forwards and backwards
Input x | Output | Explanation |
|---|---|---|
| 121 | true | |
| -121 | false | Negative numbers have a leading '-' that no digit can match |
| 10 | false | Ends in 0 but doesn't start with 0 |
| 0 | true | A single digit is a palindrome |
| 12321 | true | Odd number of digits; the middle digit is ignored |
| 1221 | true | Even number of digits |
| 123 | false | |
| 2147447412 | true | Close to INT_MAX; reversing only half never overflows |
(Confirm: x is a 32-bit signed integer; solve without converting to a string.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in