www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Palindrome Number

Last update: 11/27/2016 4:28:00 PM

By Fatih KABAKCI

The problem is to find out given an integer whether it is palindrome. There is a space restriction in this problem. For solution, O(n) = 1 space complexity should be performed.

Problem #9 - Palindrome Number

Description: Determine whether an integer is a palindrome. Do this without extra space.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.PalindromeNumber;

/**
 * 
 * @author fkabakci
 * Problem Description: Determine whether an integer is a palindrome. Do this without extra space.
 * O(n) = 1 - Space complexity restriction
 * Examples:
 * 
 * x = 1001 - > Palindrome
 * x = 12345 -> Not Palindrome
 * x = 121 -> Palindrome
 * 
 * Solution:
 * 1. See n how many digits x has
 * 2. Figure out c that is 10 exponential of n
 * 2. Divide x by c, then get left one
 * 3. Mod 10, then get right one
 * 4. Compare them, if not equal then return false, 
 * 		  otherwise keep it up until x is 0 by setting x as x mod c / 10 and decreasing n by 2.
 */
public class PalindromeNumber {

	public boolean isPalindrome(int x) {
		if (x < 0)
			return false;

		// how many digits
		int n = 0, xc = x;
		while (xc > 0) {
			xc = xc / 10;
			n++;
		}

		while(x != 0){
			// 10 exponential
			int c = ((int) Math.pow(10, n - 1));
			int left = x / c;
			int right = x % 10;
			if(left != right)
				return false;
			
			/* cut from both left and right
			 * x % c -> cutting left
			 *   / 10 -> cutting right
			 */
			x = (x % c) / 10;
			// 2 digit comparison in each turn
			n = n - 2;
		}
		return true;
	}
	
	public static void main(String[] args) {
		PalindromeNumber p = new PalindromeNumber();
		if(p.isPalindrome(12321))
			System.out.println("Yes");
		else
			System.out.println("No");
	}
}

Similar Problems

#5 Longest Palindromic Substring

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

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

icon

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#

Database

icon

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.

Hardware

icon

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

icon

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

icon

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

Network

icon

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

icon

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

icon

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.