DSA Tracker

Medium

Reverse Integer

A medium Math problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Math
Sheets
1
Core for
13 roles
Platform
LeetCode

The problem

Given a signed 32-bit integer x, reverse the digits of x. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0.

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-09

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.

What each operation costs

OperationTime
greatest common divisor via euclidean algorithmO(log(min(a, b)))
modular exponentiation by repeated squaringO(log n)
prime sieve of eratosthenes up to nO(n log log n)
What usually goes wrong with Math
  • Allowing 32-bit signed integer overflow during intermediate multiplications before modulo reduction is applied, producing negative or truncated results.
  • Failing to handle negative inputs in modulo arithmetic, producing negative remainders in languages that implement truncated rather than floored division.
  • Dividing by zero when the denominator or remainder becomes zero at unexpected edge boundary values like empty sets or singletons.

Which roles need this problem

Math is a core topic for these 13 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 7 more roles, including Security Engineer, Performance Engineer, Site Reliability Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Math problems

Problem set and role mapping as of .