Reverse an Integer Digits

In this article, we going to see about How to Reverse an Integer? 

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

It is one of the top Google interview question in Leetcode.

Whenever we get these questions, we have to be careful to handle all the cases. 

There are three cases mostly involved, to solve these types of problems.

  1. Empty
  2. Normal
  3. Edge

Case 1: Empty

What happens if no input is provided, what if the given input is null or empty.

Case 2: Normal

This is normal case, solve the problem based on the given input.

Case 3: Edge

What happens if given input has negative value, how to handle the negative cases. In this problem, one more important edge case is there.

What happens if a number overflows an integers max value and min value?

That is while reversing number, it gets overflows, that means, it will exceed the int max value and min value.

You may ask how an given input integer number will overflow when reversing that number. Let’s see below,

If our given input is 2147483647, when I reverse this number, see carefully, it becomes 7463847412. Since, reversed number exceeded the Integer.MAX_VALUE (2147483647).

As per given problem, whenever a reversed number exceeds the Integer.MAX_VALUE or Integer.MAX_VALUE, we have to return 0. This is one of the silent edge case in our given problem, How to reverse an integer.

Approach 1

It is the normal approach. Convert the integer into string and reverse the string.

Yes, converting integer to string and reversing is good idea, but most of the interviewers will ask you to don’t use built in methods, try to come with your own logic, how you will approach and solve this problem.

So we going to solve this problem by using approach 2, using % and / operators.

Approach 2

Basic knowledge

To solve this problem, you must have a good knowledge of how % and / operator works with the integer. Look at below


I’m going to use long data type to capture whenever a reversed overflow the Integer.MAX_VALUE and Integer.MIN_VALUE.

I used long because, long has more range than the int has. Here, I used long as a flag to check whenever a reversed number exceeds the Integer.MAX_VALUE and Integer.MIN_VALUE.

See the below program,

Java Program

public int reverse(int x) 
        long reversedNumber= 0;
        while( x != 0)
           int last = x % 10;
           reversedNumber = reversedNumber *10 + last;
           x= x/10;
           //Checking edge case for overflows
            if(reversedNumber > Integer.MAX_VALUE || reversedNumber < Integer.MIN_VALUE)
              return 0;
        return (int) reversedNumber;

Time Complexity : O(n)

Space Complexity : O(1)

Leave a Comment

Your email address will not be published. Required fields are marked *