Check a Decimal Integer for Symmetry
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Check whether a signed integer reads the same in both directions in decimal notation, then consider the optional challenge of avoiding string conversion.
Constraints
- -2^31 <= number <= 2^31 - 1
- Negative integers are never palindromes because the minus sign is part of the representation
- Reversing every digit of some valid inputs exceeds 2^31 - 1, so a full reversal needs a 64-bit intermediate
Examples
Input: (0,)
Expected Output: True
Input: (7,)
Expected Output: True
Hints
- Settle the cases the statement already decides first: what does a negative number return, and what about a positive number whose last digit is 0?
- The last digit of a number is number % 10 and number / 10 (integer division) drops it. You can peel digits off the right end one at a time.
- You do not need to reverse every digit. If you move digits from the right end onto a second number until the second number is at least as large as what is left, how should the two halves compare for even and odd digit counts?
Community answers
Answer by Coderka14
Answer by jeevanhu
Answer by wdi109507
Answer by boyfutureboy75
Answer by ishrox