Skip to main content

Reverse Integer - Video Solutions

MediumMath

Reverse Integer - Bit Manipulation - Leetcode 7 - Python

NeetCode
13:12132,755 views
10 video solutions available

Reverse Integer - Video Solution

Watch 10 video solutions for Reverse Integer, a medium level problem involving Math. This walkthrough by NeetCode has 132,755 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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.