Reverse Integer

MediumMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a signed 32-bit integer x. Produce the integer whose decimal digits are the digits of x in reverse order, keeping the original sign. Leading zeros that appear after reversing are dropped (so 120 becomes 21). If the reversed value falls outside the signed 32-bit range [-2^31, 2^31 - 1], return 0 instead. Assume your environment cannot store 64-bit integers: detect overflow before it happens rather than computing the full value and checking afterwards.

Examples

Input: x = 123
Output: 321
The digits 1, 2, 3 read backwards give 321.
Input: x = -123
Output: -321
The sign is kept and the digits are reversed.
Input: x = 120
Output: 21
Reversing gives 021; the leading zero is dropped.

Constraints

  • -2^31 <= x <= 2^31 - 1
  • Return 0 if the reversed value does not fit in a signed 32-bit integer.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems