Check a Decimal Integer for Symmetry

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.

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) */
Google logo
Google
Oct 20, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

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; 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.