Skip to main content

A Number After a Double Reversal - Solution & Explanation

EasyMath12 min readAsked at: Amazon, Meta, Google +1
Practice this problem

Problem Statement

Reversing an integer means to reverse all its digits.

  • For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.

Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.

 

Example 1:

Input: num = 526
Output: true
Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.

Example 2:

Input: num = 1800
Output: false
Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.

Example 3:

Input: num = 0
Output: true
Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.

 

Constraints:

  • 0 <= num <= 106

Approach Overview

Problem Overview: You’re given a non‑negative integer num. Reverse its digits once, then reverse the result again. The task is to check whether the final value equals the original number. The catch comes from how integer reversal handles leading zeros.

When a number is reversed, any leading zeros disappear. For example, reversing 120 produces 21. Reversing 21 again gives 12, which is different from the original 120. This behavior drives the entire solution.

Approach 1: Direct Reversal Approach (O(d) time, O(1) space)

This approach simulates exactly what the problem describes. Write a helper function that reverses an integer by repeatedly extracting digits using % 10 and building the reversed value with multiplication by 10. First reverse num, then reverse the result again. Finally compare the double‑reversed value with the original number.

The algorithm performs digit operations in a loop until the number becomes zero. If the number has d digits, each reversal costs O(d), making the total still O(d). Space remains O(1) since only a few integer variables are used. This approach is straightforward and mirrors the problem statement, making it a good baseline when reasoning about math operations and digit manipulation.

Approach 2: Optimized Special Case Handling (O(1) time, O(1) space)

The key insight: only numbers with trailing zeros change after a double reversal. If a number ends with 0, the first reversal removes that zero because integers cannot store leading zeros. Once the zero disappears, the second reversal cannot restore it.

Example: num = 120. First reversal → 21. Second reversal → 12. The original value cannot be recovered. In contrast, a number like 123 becomes 321 and then 123, which matches the original.

This leads to a constant‑time rule: return true if num == 0 or num % 10 != 0. Zero is a special case because reversing it still gives zero. This approach relies purely on digit properties rather than explicit reversal, a common trick in math and simulation problems.

Recommended for interviews: Start with the direct reversal simulation to demonstrate understanding of digit operations. Then point out the trailing‑zero observation and switch to the constant‑time rule. Interviewers usually expect the optimized O(1) solution because it shows you recognized the mathematical property behind the operation rather than blindly simulating it.

Approach 1: Direct Reversal Approach

This approach involves reversing the number, then reversing the result again. We first reverse the input number and store the reverse result. Then, we reverse the reversed result to see if we get back the original number.

The C solution uses a helper function reverse to reverse the digits of the number. The main function isSameAfterReversals takes an integer, reverses it once to get reversed1, and then reverses it again to get reversed2. It compares the result with the original number and returns true if they match, and false otherwise.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(d), where d is the number of digits in the number. We traverse each digit to reverse it.
Space Complexity: O(1), as we use a constant amount of space.

Try this approach in the editor →

Approach 2: Optimized Special Case Handling

In this approach, we directly check for conditions when reversing a number twice will not change it. If a number has trailing zeros, reversing it will change its length because the trailing zeros are lost. For any number except zero, if it has no trailing zeros, reversing twice will always result in the original number.

In this C implementation, we directly return true for zero, because reversing 0 gives 0 again. For other numbers, we simply check if they end with zero, as losing the trailing zero changes the number during reversal.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), as we perform a constant-time check.
Space Complexity: O(1), since no additional space is needed.

Try this approach in the editor →

Approach 3: 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.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Direct Reversal Approach

Time Complexity: O(d), where d is the number of digits in the number. We traverse each digit to reverse it.
Space Complexity: O(1), as we use a constant amount of space.

Optimized Special Case Handling

Time Complexity: O(1), as we perform a constant-time check.
Space Complexity: O(1), since no additional space is needed.

Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Reversal ApproachO(d)O(1)When implementing the exact process described or practicing digit manipulation logic.
Optimized Special Case HandlingO(1)O(1)Best solution for interviews and production code; uses the trailing-zero insight instead of full reversal.

Video Solution

A Number After a Double Reversal | Leetcode 2119 | Live coding session 🔥🔥🔥🔥| 1 Liner | Contest 273 • Coding Decoded • 703 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is A Number After a Double Reversal easy or hard?
LeetCode classifies this problem as Easy with an acceptance rate above 80%. The challenge is recognizing the trailing-zero edge case rather than implementing complex algorithms.
A Number After a Double Reversal Python/Java solution
In Python or Java, the optimized solution is a single condition: return num == 0 or num % 10 != 0. A more verbose solution can implement a helper function that reverses digits twice and compares the final value with the original number.
How to solve A Number After a Double Reversal in O(1)?
Use the trailing-zero observation. If a number ends with zero, reversing it removes that zero and the second reversal cannot restore it. Therefore return true if num == 0 or num % 10 != 0. This avoids performing any digit reversal and works in constant time.
What is the best approach for A Number After a Double Reversal?
The optimal approach checks whether the number ends with a zero. If num == 0 or num % 10 != 0, the number remains unchanged after two reversals. Any positive number ending in 0 loses that zero during the first reversal, so the final value differs. This solution runs in O(1) time and O(1) space.
Is A Number After a Double Reversal asked at Google/Amazon/Meta?
The problem represents a common screening-style question focused on number manipulation and edge-case reasoning. Similar math and digit-reversal problems appear in coding interviews at companies like Amazon and Google, especially in early interview rounds.
What data structure is used in A Number After a Double Reversal?
No complex data structure is required. The problem relies purely on integer arithmetic and digit properties. Operations such as modulo (% 10) and integer division (/ 10) are used when implementing the reversal simulation.
What is the time complexity of A Number After a Double Reversal?
The optimal solution runs in O(1) time because it only checks a simple condition on the last digit of the number. A simulation approach that reverses digits twice takes O(d) time, where d is the number of digits, since each digit must be processed during reversal.

Ready to solve this problem?

Practice A Number After a Double Reversal with our built-in code editor and test cases.

Practice on FleetCode