7. Reverse Integer
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The core task is to reverse the digits of a given 32-bit signed integer. The main challenge lies in handling the constraint that the reversed value must stay within the 32-bit signed integer range, which is [-2^31, 2^31 - 1]. If the reversed number exceeds this range, we must return 0. A crucial part of the problem is the assumption that we cannot use 64-bit integers, which means we cannot simply reverse the number and then check if it's too large. We need to detect the overflow/underflow before it happens.
Solution
Reverse Integer in Python
class Solution:
def reverse(self, x: int) -> int:
# Define the 32-bit signed integer range
INT_MAX = 2**31 - 1
INT_MIN = -2**31
reversed_num = 0
while x != 0:
# Handle Python's specific behavior for % with negative numbers.
# We need the remainder to have the same sign as the dividend.
if x > 0:
pop = x % 10
else:
pop = -(-x % 10)
# Use truncation towards zero for division, same as C++/Java.
# x // 10 floors, so int(x/10) is needed for negative numbers.
x = int(x / 10)
# Check for overflow and underflow before the multiplication
# For positive overflow, reversed_num must not exceed INT_MAX
# INT_MAX is 2147483647
if reversed_num > INT_MAX // 10 or (reversed_num == INT_MAX // 10 and pop > 7):
return 0
# For negative underflow, reversed_num must not be less than INT_MIN
# INT_MIN is -2147483648
if reversed_num < int(INT_MIN / 10) or (reversed_num == int(INT_MIN / 10) and pop < -8):
return 0
reversed_num = reversed_num * 10 + pop
return reversed_num
Reverse Integer in Java
class Solution {
public int reverse(int x) {
int reversed = 0;
while (x != 0) {
// pop operation
int pop = x % 10;
x /= 10;
// Check for overflow before pushing the digit.
// Integer.MAX_VALUE is 2147483647
if (reversed > Integer.MAX_VALUE / 10 || (reversed == Integer.MAX_VALUE / 10 && pop > 7)) {
return 0;
}
// Check for underflow before pushing the digit.
// Integer.MIN_VALUE is -2147483648
if (reversed < Integer.MIN_VALUE / 10 || (reversed == Integer.MIN_VALUE / 10 && pop < -8)) {
return 0;
}
// push operation
reversed = reversed * 10 + pop;
}
return reversed;
}
}
Reverse Integer in C++
class Solution {
public:
int reverse(int x) {
int reversed = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
// Check for overflow before multiplication.
// INT_MAX is 2147483647
if (reversed > INT_MAX / 10 || (reversed == INT_MAX / 10 && pop > 7)) {
return 0;
}
// Check for underflow before multiplication.
// INT_MIN is -2147483648
if (reversed < INT_MIN / 10 || (reversed == INT_MIN / 10 && pop < -8)) {
return 0;
}
reversed = reversed * 10 + pop;
}
return reversed;
}
};
Reverse Integer in TypeScript
function reverse(x: number): number {
const INT_MAX = 2**31 - 1; // 2147483647
const INT_MIN = -(2**31); // -2147483648
let reversed = 0;
while (x !== 0) {
// Pop the last digit. JavaScript's % operator gives a result with the same sign as the dividend.
const pop = x % 10;
// Remove the last digit. Math.trunc correctly truncates toward zero.
x = Math.trunc(x / 10);
// Check for positive overflow.
if (reversed > Math.trunc(INT_MAX / 10) || (reversed === Math.trunc(INT_MAX / 10) && pop > 7)) {
return 0;
}
// Check for negative underflow.
if (reversed < Math.trunc(INT_MIN / 10) || (reversed === Math.trunc(INT_MIN / 10) && pop < -8)) {
return 0;
}
reversed = reversed * 10 + pop;
}
return reversed;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.