Reverse digits of an integer
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Reversing digits of an integer is a classic interview and fundamentals problem. It tests arithmetic operations, sign handling, and boundary checks in a small piece of code. A strong solution is short, predictable, and explicit about edge cases.
Arithmetic Approach
The arithmetic method repeatedly takes the last digit and appends it to a result number. This avoids string conversion and works in languages where numeric constraints matter.
Logic summary:
n % 10extracts the rightmost digit.reversed_num * 10 + digitshifts left and appends.n //= 10removes the processed digit.
This is easy to reason about and runs in linear time relative to digit count.
String Approach
In scripting-heavy code, the string approach can be clearer and perfectly acceptable. You still need to handle sign and leading zeros in the reversed result.
This approach is concise and often easier for beginners, but it depends on conversion to text and back.
Handling 32-bit Overflow
Some coding platforms require returning 0 when reversed value exceeds signed 32-bit range. Add the range check after each append or once at the end.
For fixed-width languages, checking during the loop is safer because overflow can happen before final assignment.
Testing Strategy and Complexity
The reversal loop processes each digit once, so time complexity is linear in the number of digits and memory usage is constant for the arithmetic approach. That makes it a reliable default for constrained environments.
A minimal test set can be written quickly:
If your platform enforces fixed-width integers, add explicit boundary tests so overflow behavior is verified instead of assumed.
Common Pitfalls
A frequent mistake is ignoring negative numbers. Reversing -123 should produce -321, not 321 or an error.
Another bug is mishandling trailing zeros. Input 1200 should become 21, because integer values do not preserve leading zeros.
Using floating-point operations is also risky. Digit reversal should use integer math to avoid precision issues.
On challenge sites, many failures come from missing overflow requirements. Always read the exact constraints and implement the required return behavior.
Finally, test minimal and maximal boundaries, not only small positive samples. Boundary tests expose silent logic errors quickly.
Summary
- Reverse digits by repeatedly extracting and appending the last digit.
- Preserve sign separately to keep the loop simple.
- String slicing is concise, while arithmetic is constraint-friendly.
- Respect problem-specific overflow rules for fixed-width integers.
- Validate with negative values, zero, trailing zeros, and boundary cases.
Related reading
- Reverse Integer leetcode -- how to handle overflow
- Reverse Sorted Dictionary in .NET
- Reverse the ordering of words in a string
- Reversible shuffle algorithm using a key
- review of a codility test - pair_sum_even_count
- RMI alternatives for bidirectional asynchronous calls and callbacks through firewalls or NAT
- Robot exploration algorithm
- Robust algorithm for chromatic instrument tuner?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.