Skip to content
Decoder

Reverse Integer

Medium

Problem

Given a signed 32-bit integer x, return it with its decimal digits reversed, keeping the sign. If the reversed value falls outside the signed 32-bit range [-2^31, 2^31 - 1], return 0.

Example 1

Input
x = 123
Output
321

Example 2

Input
x = -123
Output
-321

Example 3

Input
x = 120
Output
21
Why
the trailing zero becomes a leading zero and disappears

Example 4

Input
x = 1534236469
Output
0
Why
9646324351 does not fit

Constraints

  • -2^31 <= x <= 2^31 - 1
  • Assume you cannot store a 64-bit integer: detect overflow before it happens.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.