coding-interview|August 27, 2019|2 min read

Reverse digits of a signed integer - Leet Code Solution

TL;DR

Pop digits with modulo 10, push onto result with multiply by 10. Check for 32-bit integer overflow before each push. Handle negative sign separately.

Reverse digits of a signed integer - Leet Code Solution

Problem Statement

Given a signed integer, reverse digits of an integer.

Example 1:
	Input: 123
	Output: 321

Example 2:
	Input: -123
	Output: -321

Example 3:
	Input: 120
	Output: 21

Return 0 for Integer overflow

Algorithm

The algorithm should be simple. You fetch the last digit. For next round, you need to move this to next digit place, and add next last digit.

  • Keep a flag indicating if the number is negative. Just to negate the result while returning And, make the number positive if it is negative
  • You need to get last digit from number, and multiply last result by 10 to move it to next place.
s = s*10 + remainder;
  • You need to take care of the integer overflow.

  • We will do two calculations

    • Multiply last remainder result by 10
    • Add current remainder with above multiplication result
  • Overflow cases: Note, max limit for 64-bit integer: 2^(64-1) - 1 = 2147483647

    • case 1: Multiplication is itself overflowed: Operation we will do: s10, we can check if s10 > Integer.MAX_VALUE OR, s > Integer.MAX_VALUE/10

    • case 2: if s*10 is equal to Integer.MAX_VALUE, Which means the number will be 214748364 multiply it with 10 will give: 21474836470 So, we need to check the remainder (which is to be added), if it is greater than 7, it is an overflow.

Code

public class Q7_ReverseInteger {
	
	public int reverse(int x) {
		boolean neg = false;
		if (x < 0) {
			//negative number
			neg = true;
			x = -x;
		}
		int s = 0;
		while (x > 0) {
			int rem = x%10;
			
			if (s > Integer.MAX_VALUE/10 || (s == Integer.MAX_VALUE/10 && rem > 7)) {
				return 0;
			}
			
			s = s*10 + rem;
			
			x = x/10;
		}
		
		if (neg) {
			return -s;
		}
		return s;
	}
}

Complexity

It is equal to the number of digits of the number. O(l)

Related Posts

Replace all spaces in a string with %20

Replace all spaces in a string with %20

Problem Statement Replace all spaces in a string with ‘%20’ (three characters…

Valid Palindrome - Leet Code Solution

Valid Palindrome - Leet Code Solution

Problem Statement Given a string, determine if it is a palindrome, considering…

Valid Anagrams - Leet Code Solution

Valid Anagrams - Leet Code Solution

Problem Statement Given two strings s and t , write a function to determine if t…

First Unique Character in a String - Leet Code Solution

First Unique Character in a String - Leet Code Solution

Problem Statement Given a string, find the first non-repeating character in it…

Reverse String - Leet Code Solution

Reverse String - Leet Code Solution

Problem Statement Write a function that reverses a string. The input string is…

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…