DSA Tracker

Problem

Medium

Reverse Integer

A medium Math interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Math
Sheets
1
Core for
13 roles

The problem

Write the digits of a 32-bit signed integer x in the opposite order, keeping its sign. If the reversed number no longer fits between -2^31 and 2^31 - 1, return 0 instead.

Example 1

Input
x = 123
Output
321
Why
Reversing the digits of 123 gives 321, which is within the 32-bit range.

Example 2

Input
x = -123
Output
-321
Why
Reversing the digits of -123 gives -321, which is within the 32-bit range.

Example 3

Input
x = 120
Output
21
Why
Reversing 120 gives 21 (the leading zero is dropped).

Example 4

Input
x = 1534236469
Output
0
Why
Reversing gives 9646324351, which exceeds 2^31 - 1 = 2147483647, so return 0.

Constraints

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

How to think about it

Updated 2026-09-29

The critical challenge is not digit extraction, but detecting whether multiplying the running reversed value by 10 will spill outside 32-bit signed limits before the operation actually takes place. The overflow check must happen prior to each append.

Approaches, worst first

  1. String conversion with bounds parsing

    time O(log10(|x|)) · space O(log10(|x|))

    Convert the absolute value to string, reverse it, restore the sign, and parse back. If the parsed value lies outside [-2^31, 2^31 - 1], return 0. It requires extra memory and relies on 64-bit parsing or string comparison logic.

  2. Digit extraction with prefix boundary checkWrite this one

    time O(log10(|x|)) · space O(1)

    Iteratively pop x % 10 and push it onto the result. Before multiplying result by 10, verify whether result exceeds INT_MAX / 10 (or is equal with a trailing digit exceeding 7) or drops below INT_MIN / 10. If so, return 0 immediately.

Where people lose marks · 2
  • In C++ or Java, `INT_MIN % 10` evaluates to -8; if taking `Math.abs(x)` up front, `Math.abs(-2^31)` overflows because 2^31 cannot be represented in a 32-bit signed integer.
  • Checking overflow after multiplying by 10 already invokes undefined behavior or silent wrap-around in fixed-width integer environments.

The theory behind it

Math — the ground this problem stands on. All Math problems

What Math is

Algorithmic math is the application of number properties, modular arithmetic, and geometric patterns to compute results without simulating every step. Rather than filling memory buffers with millions of simulated counters or walking endless loops, mathematical formulation uses identities like prime factorization, digit extraction, and greatest common divisors to jump directly to target answers through closed-form rules.

When to reach for it

Reach for mathematical techniques when problem bounds are massive, such as constraints reaching ten to the eighteenth power where simulation is impossible. Strong prompts mention prime generation, greatest common divisor calculations, modular inverses, fast exponentiation, digit reversals, or combinatorial arrangements. If brute-force counting produces memory overflow or time limit exceeded warnings, algebraic reorganization is the intended path.

How the pattern works

Focus on numeric constraints and potential overflows before writing expressions. Break numbers into constituent digits using modulo ten to peel least significant figures and integer division to shrink scale. For divisibility, apply Euclid's remainder theorem repeatedly until remainder drops to zero. Apply modulo arithmetic at every intermediate addition and multiplication stage rather than once at the end, preventing values from spilling beyond standard numeric register limits.

More Math problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .