Quick Overview

Check whether a signed integer reads the same in both directions in decimal notation, then consider the optional challenge of avoiding string conversion.

Check a Decimal Integer for Symmetry

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Decide whether the decimal representation of the signed integer `number` reads identically from left to right and from right to left. Return `true` when it does and `false` when it does not. Use the integer's usual decimal notation. A minus sign is part of the representation, so negative integers do not qualify. Zero and positive single-digit integers do qualify. ### Examples **Example 1** ```text Input: number = 4004 Output: true ``` Reading the four digits backward produces `4004` again. **Example 2** ```text Input: number = -343 Output: false ``` Reversing the representation moves the minus sign to the end; it does not reproduce the original notation. ### Constraints - `-2^31 <= number <= 2^31 - 1` ### Optional Follow-up Can you preserve the same behavior without converting `number` to a string? This restriction applies only to the follow-up.

Overview: Check whether a signed integer reads the same in both directions in decimal notation, then consider the optional challenge of avoiding string conversion.

Given a signed 32-bit integer `number`, decide whether its decimal representation reads the same from left to right as from right to left. Return `true` if it does and `false` otherwise. Use the integer's usual decimal notation (no leading zeros, no plus sign): - A minus sign is part of the representation, so every negative integer returns `false`: reading `-121` backward gives `121-`. - `0` and every positive single-digit integer return `true`. - A positive integer ending in `0` (such as `10` or `100`) returns `false`, because its notation never starts with `0`. **Example 1** ``` Input: number = 4004 Output: true ``` Reading the four digits backward produces `4004` again. **Example 2** ``` Input: number = -343 Output: false ``` Reversing the representation moves the minus sign to the end, so it does not reproduce `-343`. **Constraints** - `-2^31 <= number <= 2^31 - 1` Note: reversing all the digits of some valid inputs gives a value above `2^31 - 1` (for example, `2147483647` reversed is `7463847412`), so a solution that rebuilds the whole reversed number in a 32-bit type can overflow. **Follow-up (optional):** can you solve it without converting `number` to a string? This restriction applies only to the follow-up; the grader accepts any correct approach.

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

  1. 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?
  2. 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.
  3. 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

#include using namespace std; class Solution { public: bool solution(int number) { if (number < 0) { return false; } if (number != 0 && number % 10 == 0) { return false; } int reversedHalf = 0; while (number > reversedHalf) { int digit = number % 10; reversedHalf = reversedHalf * 10 + digit; number /= 10; } return number == reversedHalf || number == reversedHalf / 10; } }; /* Approach: Reverse Half of the Number Negative numbers cannot be palindromes. Numbers ending in 0 cannot be palindromes, except 0 itself. Reverse only the second half of the number. For even digits, compare both halves directly. For odd digits, ignore the middle digit using reversedHalf / 10. Time Complexity: O(log10(N)) Auxiliary Space: O(1) */

Answer by jeevanhu

def is_symmetric_integer(number: int) -> bool: """ Checks if a decimal integer is symmetric (a palindrome) without using string conversion. """ # Negative numbers are not symmetric due to the minus sign. # Also, if the last digit is 0, the first digit must also be 0. # Only 0 satisfies this property. if number < 0 or (number % 10 == 0 and number != 0): return False reversed_half = 0 # Reverse the digits of the number until the reversed half is greater # than or equal to the remaining original number. while number > reversed_half: reversed_half = reversed_half * 10 + number % 10 number //= 10 # When the length is an odd number, we can get rid of the middle digit by reversed_half // 10. # For example when the input is 12321, at the end of the while loop we get number = 12, reversed_half = 123, # since the middle digit doesn't matter in a palindrome(it will always equal to itself), we can safely get rid of it. return number == reversed_half or number == reversed_half // 10

Answer by wdi109507

class Solution: def follow_up(self, x: int) -> bool: reversed_digit = 0 if x < 0 or (x != 0 and x % 10 == 0): return False while x > reversed_digit: digit = x % 10 x //= 10 reversed_digit = reversed_digit * 10 + digit if x == reversed_digit or x == reversed_digit // 10: return True return False

Answer by boyfutureboy75

def solution(number): if number < 0: return False return str(number) == str(number)[::-1]

Answer by ishrox

public class Solution { public static boolean solution(int number) { if (number < 0) { return false; } String str = String.valueOf(number); String reversed = new StringBuilder(str).reverse().toString(); return str.equals(reversed); } }

Loading coding console...

Show the approach

Approach

Negative numbers return false immediately, because the leading minus sign cannot appear at the end of a reversed reading. A positive number whose last digit is 0 also returns false, since its first digit is never 0; this check matters for the loop below, which would otherwise treat 10 as matching. For the rest, repeatedly take the last digit of number (number % 10), append it to reversed_half (reversed_half * 10 + digit), and drop it from number (integer division by 10). Stop once number is no longer larger than reversed_half: at that point about half the digits have moved across, in reverse order. With an even digit count the number is a palindrome exactly when the two halves are equal (4004 splits into 40 and 40). With an odd digit count reversed_half holds the middle digit as its last digit, so divide it by 10 before comparing (12321 splits into 12 and 123). Because only about half the digits are reversed, reversed_half stays far below 2^31 - 1 and no 64-bit intermediate is needed, and no string conversion is used, which answers the follow-up.

Time complexity:
O(log10 |number|), at most about 10 loop iterations for a 32-bit input
Space complexity:
O(1)