Personal Website and Computer Science TUR EN

Interview Question Reverse Integer

Last update: 11/20/2016 5:03:00 PM


The problem is to reverse an integer that is entered by the user. The main points of the solution are to check last and first characters. First, if the last character is already 0, then the result should be 0 too. On the other hand, first character might be entered by adding either + or - operator. In this case, first character is ignored when the integer is being converted to the string. The basic solution has been shared the following.

Problem #7 - Reverse Integer

Description: Reverse digits of an integer.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.ReverseInt;

 * @author fkabakci
 * Problem Description: Reverse digits of an integer.
 * Examples:
 * Example1: x = 123, return 321
 * Example2: x = -123, return -321
 * Solution:
 * 1. Convert it to String.
 * 2. Check the last character if it is 0, if so return 0.
 * 3. Check the first character if it has + or - operator, if so, then cut the first one in string, 
 *    and specify it positive or negative.
 * 4. Reverse all characters
 * 5. Convert it to Integer, then return the result. If overflow occurs return 0.
public class ReverseInteger {
	public int reverse(int x) {
		String sx = String.valueOf(x);
		// last character if 0 ?
		char cn = sx.charAt(sx.length() - 1);
		if (cn == "0")
			return 0;

		short pre = 1;
		// first character + or - ?
		char c0 = sx.charAt(0);
		if (c0 == "+") {
			pre = 1;
			sx = sx.substring(1);
		} else if (c0 == "-") {
			pre = -1;
			sx = sx.substring(1);

		char[] cx = sx.toCharArray();
		char[] rx = new char[cx.length];
		for (int i = cx.length - 1; i >= 0; i--) {
			// reversing
			rx[cx.length - 1 - i] = cx[i];
		try {
			int result = Integer.valueOf(new String(rx));

			return pre * result;
		} catch (NumberFormatException nfe) {
			return 0;

	public static void main(String[] args) {
		ReverseInteger r = new ReverseInteger();

The algorithm complexity is O(n) because of n character.

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.