Skip to main content

Reverse Integer - Solution & Explanation

MediumMath25 min readAsked at: Amazon, Microsoft, Apple +19
Practice this problem

Problem Statement

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

 

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

 

Constraints:

  • -231 <= x <= 231 - 1

Approach Overview

Problem Overview: Reverse the digits of a signed 32-bit integer x. If reversing the digits causes the value to go outside the 32-bit signed integer range [-2^31, 2^31 - 1], return 0. Negative numbers must preserve their sign after reversal.

Approach 1: Iterative Division and Modulus (Time: O(d), Space: O(1))

This approach processes the integer digit by digit using arithmetic operations. Repeatedly extract the last digit using x % 10, append it to a new number using rev = rev * 10 + digit, and remove the last digit from the original number with integer division x //= 10. The key challenge is detecting overflow before it happens. You check whether rev will exceed the 32-bit boundary before multiplying by 10. This solution uses constant space and avoids converting the number to a string, making it the standard arithmetic solution expected in interviews involving math and integer manipulation.

Approach 2: String Conversion and Manipulation (Time: O(d), Space: O(d))

This approach converts the integer to a string, reverses the characters, and converts the result back to an integer. Handle the negative sign separately: store it, reverse the remaining digits, then reapply the sign. After conversion, verify the result still falls within the 32-bit signed integer range. This method is simpler to implement and easier to reason about, but it uses extra memory for the string and relies on built-in string operations from languages such as Python or JavaScript. It demonstrates basic manipulation of strings rather than pure math operations.

Recommended for interviews: The iterative division and modulus approach is the one interviewers expect. It shows you understand integer digit extraction, overflow detection, and constant-space arithmetic. The string method is acceptable for quick implementations, but the arithmetic solution demonstrates stronger algorithmic control over numeric operations.

Approach 1: Iterative Division and Modulus

This method involves extracting digits one at a time from the integer using modulus and division operations. By repeatedly taking the last digit of the number, you can construct the reversed integer. However, special care must be taken to handle the overflow condition, as reversing an integer can potentially produce a value beyond the 32-bit signed integer limit.

This C solution uses a while loop to pop digits from the input number x and push them onto the result. Overflow conditions are checked both before multiplication and addition.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log10(x)) because we are processing each digit once.
Space Complexity: O(1) since we are using constant extra space.

Try this approach in the editor →

Approach 2: String Conversion and Manipulation

In this approach, the integer is first converted to a string representation, reversed as a string, and then converted back to an integer. This method mitigates overflow issues by checking the integer post-conversion if it falls within the valid range.

This Python solution converts the integer x to a string, reverses it, and then converts it back to an integer. The sign is preserved and checked for overflow before returning the result.

Code

Python

JavaScript

Complexity

Time Complexity: O(n) where n is the number of digits in x.
Space Complexity: O(n) due to the use of the string to store digits temporarily.

Try this approach in the editor →

Approach 3: Mathematics

Let's denote mi and mx as -2^{31} and 2^{31} - 1 respectively, then the reverse result of x, ans, needs to satisfy mi \le ans \le mx.

We can continuously take the remainder of x to get the last digit y of x, and add y to the end of ans. Before adding y, we need to check if ans overflows. That is, check whether ans times 10 + y is within the range [mi, mx].

If x \gt 0, it needs to satisfy ans times 10 + y leq mx, that is, ans times 10 + y leq \left \lfloor \frac{mx}{10} \right \rfloor times 10 + 7. Rearranging gives (ans - \left \lfloor \frac{mx}{10} \right \rfloor) times 10 leq 7 - y.

Next, we discuss the conditions for the inequality to hold:

  • When ans \lt \left \lfloor \frac{mx}{10} \right \rfloor, the inequality obviously holds;
  • When ans = \left \lfloor \frac{mx}{10} \right \rfloor, the necessary and sufficient condition for the inequality to hold is y leq 7. If ans = \left \lfloor \frac{mx}{10} \right \rfloor and we can still add numbers, it means that the number is at the highest digit, that is, y must not exceed 2, therefore, the inequality must hold;
  • When ans \gt \left \lfloor \frac{mx}{10} \right \rfloor, the inequality obviously does not hold.

In summary, when x \gt 0, the necessary and sufficient condition for the inequality to hold is ans leq \left \lfloor \frac{mx}{10} \right \rfloor.

Similarly, when x \lt 0, the necessary and sufficient condition for the inequality to hold is ans geq \left \lfloor \frac{mi}{10} \right \rfloor.

Therefore, we can check whether ans overflows by checking whether ans is within the range [\left \lfloor \frac{mi}{10} \right \rfloor, \left \lfloor \frac{mx}{10} \right \rfloor]. If it overflows, return 0. Otherwise, add y to the end of ans, and then remove the last digit of x, that is, x \gets \left \lfloor \frac{x}{10} \right \rfloor.

The time complexity is O(log |x|), where |x| is the absolute value of x. The space complexity is O(1).

Code

Python

Java

C++

Go

Rust

JavaScript

C#

C

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Division and Modulus

Time Complexity: O(log10(x)) because we are processing each digit once.
Space Complexity: O(1) since we are using constant extra space.

String Conversion and Manipulation

Time Complexity: O(n) where n is the number of digits in x.
Space Complexity: O(n) due to the use of the string to store digits temporarily.

Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Division and ModulusO(d)O(1)Preferred interview solution. Uses arithmetic operations and handles overflow explicitly.
String Conversion and ManipulationO(d)O(d)Quick implementation when readability matters more than strict constant space.

Video Solution

Reverse Integer - Bit Manipulation - Leetcode 7 - Python • NeetCode • 132,755 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reverse Integer easy or hard?
Reverse Integer is generally classified as a medium problem because of the overflow constraint. Reversing digits itself is simple, but correctly detecting overflow without using larger numeric types requires careful boundary checks.
Reverse Integer Python/Java solution
In Python or Java, the arithmetic solution repeatedly extracts digits using modulus and builds the reversed number with multiplication by 10. Java implementations must explicitly check 32-bit overflow using Integer.MAX_VALUE and Integer.MIN_VALUE. Python still needs manual boundary checks to match the problem constraints.
How to solve Reverse Integer in O(n)?
Treat n as the number of digits in the integer. Repeatedly extract digits using modulus, build the reversed number using multiplication by 10, and check for overflow before each update. The loop runs once per digit, giving O(n) time and O(1) extra space.
What is the best approach for Reverse Integer?
The best approach uses iterative division and modulus. Extract the last digit with x % 10, append it to the reversed number using rev = rev * 10 + digit, and shrink the original number with integer division. Add overflow checks before multiplying by 10. This runs in O(d) time and O(1) space where d is the number of digits.
Is Reverse Integer asked at Google/Amazon/Meta?
Reverse Integer is a common screening-level problem used by companies like Amazon, Google, and Meta to test understanding of integer manipulation, overflow handling, and basic algorithmic reasoning. Variants appear in phone screens and coding assessments.
What data structure is used in Reverse Integer?
The optimal solution does not require any data structure. It relies purely on arithmetic operations such as modulus and integer division. The alternative implementation uses strings for digit reversal, which introduces extra memory usage.
What is the time complexity of Reverse Integer?
The time complexity is O(d), where d is the number of digits in the integer. Each iteration processes one digit using modulus and division. Since a 32-bit integer has at most 10 digits, the operation effectively runs in constant time.

Ready to solve this problem?

Practice Reverse Integer with our built-in code editor and test cases.

Practice on FleetCode