Reverse the digits of an integer
Coding problemEasyAsked in: Capgemini, Infosys, Wipro
Given a 32-bit signed integer n, return the number whose digits are the digits of n in reverse order, with the same sign as n.
Zeros at the end of n become leading zeros and disappear: 120 gives 21. If the reversed value does not fit in a 32-bit signed integer (from -2,147,483,648 to 2,147,483,647), return 0.
Examples
Example 1
Input: n = 123
Output: 321
Example 2
Input: n = -450
Output: -54
Example 3
Input: n = 1534236469
Output: 0
The reverse, 9,646,324,351, is too large for 32 bits.
Constraints
- n is between -2,147,483,648 and 2,147,483,647
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.
to answer and keep your progress. Reading is free for everyone.