Ctrl K

Backwards Digits in 32 Bits

Medium
You are given an integer num that fits in a signed 32-bit integer. Write its digits in reverse order, keep the sign, and return the result. If the reversed number falls outside [-2³¹, 2³¹ − 1], return 0. Assume you cannot store 64-bit numbers: catch the overflow before it happens.
Example 1
num=4071 → 1704

Digits 4, 0, 7, 1 read backwards give 1704.

Example 2
num=-560 → -65

The sign stays, and the trailing zero becomes a leading one and disappears: -65.

Example 3
num=1999999999 → 0

Reversed it is 9999999991, which exceeds 2³¹ − 1, so the answer is 0.

Constraints
  • -2³¹ ≤ num ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings MediumTurn a Square Clockwise Medium
loading Python…
Input
num=4071
Expected
1704

Run checks the first 3 tests in your browser. Submit checks all 54 tests on the server, hidden ones included.