v9.1.1
Menu

Reverse Integer

mediumPROMath & DesignMicrosoft DSA

Given a signed 32-bit integer, return it with its digits reversed. Return 0 if the result overflows 32-bit range.

Example

Input
123
Output
321
Reasoning
  • The input integer is 123123 and we need to reverse its digits.
  • We convert the integer into a string or a list of digits to easily reverse them, resulting in [3,2,1][3, 2, 1].
  • Then, we join the reversed digits and convert them back to an integer, giving us 321321.
  • Since 321321 is within the 32-bit signed integer range of −231-2^{31} to 231−12^{31} - 1, we return 321321 as the result.

Constraints

  • -2^31 <= x <= 2^31 - 1

More from Microsoft DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.