Reverse Integer
Java coding interview problem for Basic Number Programs: Reverse Integer.
Reversing an integer is one of the most frequently asked Java coding interview questions. It tests your understanding of arithmetic operations, loops, integer manipulation, and edge case handling.
This problem is commonly asked in interviews at companies like Amazon, Oracle, IBM, Microsoft, Google, and many financial institutions.
Problem Statement
Given an integer, reverse its digits and return the reversed integer.
Examples
Example 1
Input
12345
Output
54321
Example 2
Input
9870
Output
789
Leading zeros are automatically removed.
Example 3
Input
-456
Output
-654
The negative sign remains unchanged.
Real Interview Question
Write a Java program to reverse an integer without converting it to a String.
Understanding the Logic
To reverse a number,
- Extract the last digit.
- Append it to a new number.
- Remove the last digit from the original number.
- Repeat until the original number becomes zero.
Example
1234
Step 1
Last Digit = 4
Reverse = 4
Step 2
Last Digit = 3
Reverse = 43
Step 3
Last Digit = 2
Reverse = 432
Step 4
Last Digit = 1
Reverse = 4321
Brute Force Approach
The simplest solution is
- Extract the last digit using
% - Build the reversed number
- Remove the last digit using
/ - Repeat until the number becomes zero
Algorithm
Step 1
Read the input number.
Step 2
Initialize
reverse = 0;
Step 3
Extract the last digit.
digit = number % 10;
Step 4
Append the digit.
reverse = reverse * 10 + digit;
Step 5
Remove the last digit.
number = number / 10;
Step 6
Repeat until
number = 0
Step 7
Print the reversed number.
Dry Run
Input
1234
| Iteration | Number | Digit | Reverse |
|---|---|---|---|
| 1 | 1234 | 4 | 4 |
| 2 | 123 | 3 | 43 |
| 3 | 12 | 2 | 432 |
| 4 | 1 | 1 | 4321 |
Output
4321
Dry Run (Trailing Zero)
Input
1200
| Iteration | Number | Digit | Reverse |
|---|---|---|---|
| 1 | 1200 | 0 | 0 |
| 2 | 120 | 0 | 0 |
| 3 | 12 | 2 | 2 |
| 4 | 1 | 1 | 21 |
Output
21
Trailing zeros disappear automatically.
Approach 1 — Using Arithmetic Operations
Complete Java Program
public class ReverseInteger {
public static void main(String[] args) {
int number = 12345;
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
System.out.println("Reversed Number = " + reverse);
}
}
Output
Reversed Number = 54321
Step-by-Step Code Explanation
Step 1
Read the input number.
int number = 12345;
Step 2
Initialize the reversed number.
int reverse = 0;
Initially,
Reverse = 0
Step 3
Extract the last digit.
int digit = number % 10;
Example
12345 % 10 = 5
Step 4
Append the digit to the reversed number.
reverse = reverse * 10 + digit;
Initially
0 × 10 + 5 = 5
Next iteration
5 × 10 + 4 = 54
Next
54 × 10 + 3 = 543
Eventually
54321
Step 5
Remove the last digit.
number = number / 10;
Example
12345
↓
1234
↓
123
↓
12
↓
1
↓
0
Step 6
Continue until
number == 0
When the loop finishes,
reverse
contains the reversed integer.
Why Does This Work?
Each iteration performs two important operations:
- Removes the last digit from the original number.
- Adds that digit to the end of the reversed number.
For
12345
The digits are processed as
5
↓
4
↓
3
↓
2
↓
1
Result
54321
Drawbacks of This Approach
Although this solution is efficient and widely used in interviews, it does not handle integer overflow.
For example,
2147483647
(the maximum value of an int) may overflow when reversed.
we'll improve the solution by handling overflow safely, create a reusable reverseInteger() method, explore the StringBuilder approach, analyze time and space complexity, discuss common interview mistakes, and answer frequently asked interview follow-up questions.
Approach 2 — Using a Reusable Method
Instead of writing the logic directly inside the main() method, create a reusable method.
This approach improves
- Code reusability
- Readability
- Unit testing
- Maintainability
Java Solution
public class ReverseIntegerMethod {
static int reverseInteger(int number) {
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
return reverse;
}
public static void main(String[] args) {
int number = 98765;
int result = reverseInteger(number);
System.out.println("Reversed Number = " + result);
}
}
Output
Reversed Number = 56789
Approach 3 — Handling Integer Overflow
In Java,
int
ranges from
-2,147,483,648
to
2,147,483,647
If reversing a number exceeds this range,
an integer overflow occurs.
Example
1534236469
Reverse
9646324351
This value cannot fit inside an int.
Overflow Safe Solution
public class ReverseIntegerSafe {
static int reverseInteger(int number) {
int reverse = 0;
while (number != 0) {
int digit = number % 10;
if (reverse > Integer.MAX_VALUE / 10 ||
reverse < Integer.MIN_VALUE / 10) {
return 0;
}
reverse = reverse * 10 + digit;
number = number / 10;
}
return reverse;
}
public static void main(String[] args) {
int number = 1534236469;
System.out.println(reverseInteger(number));
}
}
Output
0
Returning
0
is the expected behavior in problems like LeetCode Reverse Integer when overflow occurs.
Approach 4 — Using StringBuilder
Although simple,
this approach is usually not preferred in interviews because the interviewer often expects an arithmetic solution.
Java Solution
public class ReverseUsingString {
public static void main(String[] args) {
int number = 12345;
boolean negative = number < 0;
String value = String.valueOf(Math.abs(number));
String reversed = new StringBuilder(value)
.reverse()
.toString();
int result = Integer.parseInt(reversed);
if (negative) {
result = -result;
}
System.out.println(result);
}
}
Output
54321
Time Complexity
Arithmetic Solution
| Operation | Complexity |
|---|---|
| Time | O(log₁₀ n) |
| Space | O(1) |
StringBuilder Solution
| Operation | Complexity |
|---|---|
| Time | O(log₁₀ n) |
| Space | O(log₁₀ n) |
Comparison
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Arithmetic Operations | O(log n) | O(1) | ✅ Best |
| Reusable Method | O(log n) | O(1) | Reusable |
| Overflow Safe | O(log n) | O(1) | Production Ready |
| StringBuilder | O(log n) | O(log n) | Easy but Less Preferred |
Common Mistakes
Mistake 1
Forgetting to multiply by 10.
Wrong
reverse = reverse + digit;
Correct
reverse = reverse * 10 + digit;
Mistake 2
Using
number % 10
without removing the digit.
Always write
number = number / 10;
Otherwise,
the loop never ends.
Mistake 3
Ignoring integer overflow.
Always consider
Integer.MAX_VALUE
Integer.MIN_VALUE
for production-quality code.
Mistake 4
Using String conversion when the interviewer specifically asks
"Without converting the number into a String."
Use arithmetic operations instead.
Mistake 5
Ignoring negative numbers.
Example
-123
Expected Output
-321
The sign should remain unchanged.
Interview Follow-up Questions
Q1. Reverse an Integer without using String.
Q2. Reverse a Negative Integer.
Q3. Handle Integer Overflow while reversing.
Q4. Reverse only even digits.
Q5. Reverse only odd digits.
Q6. Reverse digits recursively.
Q7. Check whether a reversed integer is a palindrome.
Q8. Reverse a Long value.
Q9. Reverse digits using Java Streams.
Q10. Explain why % and / are used in the solution.
Related Coding Problems
- Palindrome Number
- Reverse String
- Armstrong Number
- Prime Number
- Fibonacci Series
- Count Digits
- Sum of Digits
- Decimal to Binary
Key Takeaways
- Reverse an integer by repeatedly extracting the last digit using the modulus (
%) operator. - Remove the last digit using integer division (
/). - Build the reversed number using:
reverse = reverse * 10 + digit;
- The arithmetic solution runs in O(log n) time and uses O(1) extra space.
- Handle integer overflow when working with very large values.
- The arithmetic approach is the preferred solution in Java coding interviews because it demonstrates a strong understanding of number manipulation.
Interview Tip
If an interviewer asks:
"Write a Java program to reverse an integer."
Start with the arithmetic approach using % and / operators. Explain how each digit is extracted and appended to the reversed number. Then discuss how to handle negative numbers and integer overflow, and finally mention the StringBuilder approach as an alternative while clarifying that arithmetic operations are generally preferred in interviews due to their constant space complexity and direct manipulation of numeric data.