LeetCode Problem Workspace

A Number After a Double Reversal

Determine if reversing a number twice returns the original integer by analyzing trailing zeros and digit placement.

category

1

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Math-driven solution strategy

bolt

Answer-first summary

Determine if reversing a number twice returns the original integer by analyzing trailing zeros and digit placement.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

Start by checking if the number ends with zero, which loses digits on reversal unless it is zero itself. Reverse the number once and then again to compare with the original. This problem tests understanding of digit manipulation and edge cases in math-driven reversals.

Problem Statement

Given a non-negative integer num, reversing it means writing its digits in reverse order. For example, reversing 123 becomes 321.

Your task is to reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals the original num, otherwise return false. Consider that numbers ending with zero may lose digits during reversal.

Examples

Example 1

Input: num = 526

Output: true

Reverse num to get 625, then reverse 625 to get 526, which equals num.

Example 2

Input: num = 1800

Output: false

Reverse num to get 81, then reverse 81 to get 18, which does not equal num.

Example 3

Input: num = 0

Output: true

Reverse num to get 0, then reverse 0 to get 0, which equals num.

Constraints

  • 0 <= num <= 106

Solution Approach

Check for trailing zeros

If num ends with zero and is not zero itself, return false immediately because reversing will lose trailing zeros and cannot restore the original.

Perform double reversal

Convert the number to a string, reverse it once, then reverse it again to produce reversed2. Compare reversed2 with num to determine the result.

Return comparison result

If the double-reversed number equals the original num, return true. Otherwise, return false. This handles both single-digit numbers and numbers with trailing zeros correctly.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(log(num)) because the number of digits determines reversal operations. Space complexity is O(log(num)) if using string conversion or O(1) with arithmetic reversal.

What Interviewers Usually Probe

  • Checks if you notice the impact of trailing zeros on digit reversal.
  • Tests your ability to reason about edge cases like zero and single-digit numbers.
  • Looks for a simple math-driven solution without unnecessary loops or sorting.

Common Pitfalls or Variants

Common pitfalls

  • Assuming all numbers can be restored after double reversal, ignoring trailing zeros.
  • Using arithmetic reversal without handling zero properly, causing incorrect comparison.
  • Overcomplicating the solution instead of leveraging the math-driven pattern.

Follow-up variants

  • Instead of double reversal, ask if reversing three times returns the original number.
  • Extend to signed integers and determine if negative numbers behave similarly under double reversal.
  • Determine which numbers in a range are unchanged after a double reversal.

FAQ

Why does a number ending with zero fail after double reversal?

Because reversing a number with trailing zeros removes those zeros, preventing the second reversal from restoring the original.

Can zero itself pass the double reversal test?

Yes, zero reversed twice remains zero, so the function should return true.

Does this problem pattern require string conversion?

Not necessarily; arithmetic reversal works too, but string conversion simplifies handling leading and trailing zeros.

How do single-digit numbers behave in double reversal?

Single-digit numbers remain unchanged, so they always return true under double reversal.

What is the key insight in 'A Number After a Double Reversal' problem?

Recognize that any number ending with zero (except zero itself) cannot be restored by double reversal due to lost digits.

terminal

Solution

Solution 1: Mathematics

If the number is $0$, or the last digit of the number is not $0$, then the number after reversing twice will be the same as the original number.

1
2
3
class Solution:
    def isSameAfterReversals(self, num: int) -> bool:
        return num == 0 or num % 10 != 0
A Number After a Double Reversal Solution: Math-driven solution strategy | LeetCode #2119 Easy