Watch 10 video solutions for Reverse Integer, a medium level problem involving Math. This walkthrough by CodeHelp - by Babbar has 921,595 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 - 1Problem Overview: Given a 32-bit signed integer x, reverse its digits. If reversing the number causes it to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], return 0. The main challenge is extracting digits efficiently and detecting overflow before it happens.
Approach 1: Iterative Division and Modulus (O(log n) time, O(1) space)
This method processes the integer digit by digit using arithmetic operations. Repeatedly extract the last digit using x % 10, then remove it from the number using integer division x //= 10. Build the reversed value by multiplying the current result by 10 and adding the extracted digit. The critical detail is overflow detection: before updating rev = rev * 10 + digit, check whether rev would exceed the 32-bit limit. If it does, return 0. The loop runs once per digit, giving O(log n) time since the number of digits grows logarithmically with the integer size, and O(1) space because only a few variables are used.
This approach relies purely on arithmetic and fits naturally with problems involving math operations and integer manipulation. Interviewers usually expect this version because it demonstrates control over numeric operations, edge cases, and overflow handling.
Approach 2: String Conversion and Manipulation (O(d) time, O(d) space)
Another option converts the integer to a string, reverses the characters, then converts the result back to an integer. First capture the sign, convert the absolute value of x into a string, and reverse it using slicing or built‑in utilities. After reversing, convert it back to an integer and reapply the original sign. Finally, check if the result lies within the 32-bit signed range. This method is straightforward because reversing a string is trivial, but it introduces extra memory usage proportional to the number of digits.
This technique is often used in scripting languages where string operations are convenient. It touches both string manipulation and numeric processing. The complexity remains linear in the number of digits (O(d) time) with O(d) auxiliary space for the reversed string.
Recommended for interviews: The iterative division and modulus approach is the expected solution. It shows that you understand how to extract digits, rebuild numbers, and guard against overflow in constant space. The string-based solution works and is easy to write, but interviewers often treat it as a convenience approach rather than demonstrating strong control over numeric algorithms.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Division and Modulus | O(log n) | O(1) | Preferred interview solution. Works without extra memory and handles overflow checks explicitly. |
| String Conversion and Manipulation | O(d) | O(d) | Quick implementation in languages with strong string utilities. |