Skip to content
AI360Xpert
Beta
Difficulty: MediumBit Manipulation

Reverse Integer

Problem in Plain English

Reverse the decimal digits of a signed 32-bit integer x while keeping its sign. If the mirrored digits fall outside the signed 32-bit range, return 0.

Problem Statement

Given a signed 32-bit integer x, return x with its digits reversed. A negative sign stays in front, so -123 becomes -321, and trailing zeros vanish because 120 becomes 21.

If the reversed digits fall outside the signed 32-bit range from −231-2^{31} to 231−12^{31}-1, return 0. The environment does not allow storing 64-bit integers, so the overflow must be detected before it happens.

Constraints

  • −231≤x≤231−1-2^{31} \le x \le 2^{31}-1
  • A reversal outside the signed 32-bit range returns 0.
  • No 64-bit storage may be used for the intermediate result.

Examples

Example 1

Input
x = 123
Output
321

The digits 123 mirrored give 321, which fits in the 32-bit range.

Example 2

Input
x = -123
Output
-321

The sign stays in front while the digits 123 mirror to 321.

Example 3

Input
x = 120
Output
21

Mirroring gives 021, and the leading zero drops to leave 21.

Example 4

Input
x = 0
Output
0

Zero has no nonzero digit to move, so its reversed numeric value remains zero.

Example 5

Input
x = 1534236469
Output
0

Its decimal reversal would be 9646324351, larger than 2147483647. Return zero rather than constructing that value in the solution.

Example 6

Input
x = -1463847412
Output
-2147483641

The reversed negative digits still fit above the lower bound -2147483648, so retain the sign and return -2147483641.

Intuition

Think of the input as a stack of decimal digits: taking the last digit of 123 yields 3, then 2, then 1, and appending them gives 321. A negative input must keep its sign while its digits change places. The difficult part is detecting overflow before an accumulator grows; comparing a textual candidate to the bound or guarding each arithmetic append avoids ever storing an out-of-range result.

Approaches

String Reverse

Solution Details

Reveal String Reverse: intuition, complexity, and code

Hints

Hint 1
Peel digits off the right end with x % 10 and drop them with truncating division by 10.
Hint 2
Grow the answer from the left with res = res * 10 + digit, mirroring the digit order.
Hint 3
Before the multiply-and-add step, compare res against 214748364 so the overflow is caught while every value still fits.

Edge Cases

  • Input 0 returns 0; trailing zeros vanish naturally, so 120 becomes 21 and -120 becomes -21.
  • 1534236469 and -1534236469 reverse outside the range and return 0.
  • -2147483648 cannot be safely negated in signed 32-bit arithmetic. Keep signed digits or remove its minus sign as text; its reversal is rejected.
  • 1463847412 and -1463847412 return 2147483641 and -2147483641, close to the positive and negative limits.
  • A negative one-digit input such as -7 reverses to itself; -10 returns -1.

Common Mistakes and Interview Tips

  • Checking overflow after res * 10 + digit, when the damage is already done.
  • Using x % 10 on negatives in Python, where the remainder stays non-negative and breaks the digit stream.
  • Special-casing trailing zeros instead of letting the peel-and-push math drop them into 21.
  • Using the same final digit limit for both signs ignores the asymmetric range: the positive bound ends in 7, the negative magnitude ends in 8.
  • Parsing a mirrored string before checking its range violates the intermediate-storage constraint even if a later check returns zero.
  • Interview tip: explain the quotient-and-final-digit guard first, then show how negative division differs between Python and Java/JavaScript.

Key Takeaway

Peel-and-push with % 10 and truncating division rebuilds any integer digit by digit, and bounds must be tested before the accumulator grows.