Back to course

Problem: Reverse Integer with Overflow Check

Content Reader1 words • 0:00 • Browser TTS

image.png

Introduction to DSA & Problem Solving – Reverse Integer with Overflow Check

Problem Statement

Write a function reverse(x) that takes a 32-bit signed integer and returns its digits reversed.
If the reversed value overflows the 32-bit integer range, return 0.

Requirements

  • Reverse the digits of the integer (both positive and negative values).
  • Return 0 if the reversed number exceeds the range of a 32-bit signed integer.
  • The 32-bit signed integer range is -2³¹, 2³¹ − 1-2147483648, 2147483647.

Constraints

  • Time Complexity: O(d) where d = number of digits.
  • Space Complexity: O(1) — only a few extra variables.

Examples

InputOutputExplanation
123321Digits reversed normally.
-123-321Sign preserved, digits reversed.
15342364690Overflow occurs after reversal.

Step-by-Step Approach

  1. Preserve the Original Number:
    Store the input value in xCopy to remember its original sign.
  2. Work with Absolute Value:
    Use Math.abs(x) (or abs(x) in other languages) to simplify digit extraction.
  3. Reverse the Digits:
    • Initialize rev = 0.
    • Loop while x != 0:
      1. Extract the last digit: last = x % 10
      2. Append it to rev: rev = rev * 10 + last
      3. Drop the last digit: x = Math.floor(x / 10)
  4. Check for Overflow:
    Before returning, check if rev exceeds the 32-bit signed integer limit.
    If yes → return 0.
  5. Restore the Sign:
    If the original number (xCopy) was negative, return -rev; otherwise, return rev.

Visualization

Let’s trace x = 123

Stepxlastrev (after step)
Start123-0
11233
21232
301321

Output → 321

Now for x = -123
We take the absolute value → 123
Reverse it → 321
Then reapply the sign → -321

Code Implementations

var reverse = function(x) {
  
  // Preserve the Original Number
  let xCopy = x;
  let rev = 0;
  
  // Work with Absolute Value
  x = Math.abs(x);

  // Reverse the Digits
  while (x > 0) {
    let last = x % 10;
    rev = rev * 10 + last;
    x = Math.floor(x / 10);
  }

  // Overflow check
  if (rev > 2**31 - 1) return 0;

  // Restore sign
  return xCopy < 0 ? -rev : rev;
};

console.log(reverse(123));   // 321
console.log(reverse(-123));  // -321
console.log(reverse(1534236469)); // 0 (overflow)

Key Takeaways

  • Always handle overflow for problems involving integer reversal or arithmetic.
  • Use absolute value to simplify digit extraction.
  • Preserve the original sign and reapply it after reversal.
  • The 32-bit integer limit is:
    • Minimum: -2³¹ = -2147483648
    • Maximum: 2³¹ − 1 = 2147483647
  • Time Complexity: O(d) where d = number of digits.
  • Space Complexity: O(1)
Photo of Rahul Aher

Written by Rahul Aher

I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.

More about me