Watch 10 video solutions for A Number After a Double Reversal, a easy level problem involving Math. This walkthrough by Coding Decoded has 690 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Reversing an integer means to reverse all its digits.
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 <= 106Problem 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 | Time | Space | When to Use |
|---|---|---|---|
| Direct Reversal Approach | O(d) | O(1) | When implementing the exact process described or practicing digit manipulation logic. |
| Optimized Special Case Handling | O(1) | O(1) | Best solution for interviews and production code; uses the trailing-zero insight instead of full reversal. |