Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the solution in Java.

package com.badal.careercup;

public class IntPalindrome {

private boolean isPalindrome(int n){
String test=Integer.toString(n);

if(test.charAt(0)!=test.charAt(test.length()-1)){

System.out.println("First and last digits are not same. The number is not a palindrome. ");
return false;
}


String firsthalf=test.substring(0,test.length()/2);
//System.out.println("First Half is: "+firsthalf);

String secondhalf;

if(test.length()%2==0){
secondhalf=test.substring(test.length()/2, test.length());
}else{
secondhalf= test.substring(test.length()/2+1, test.length());

}

StringBuffer sb=new StringBuffer(secondhalf);
sb=sb.reverse();

String reverseSecondhalf=sb.toString();
//System.out.println("Reverse Second Half is: "+reverseSecondhalf);

if(firsthalf.equals(reverseSecondhalf)){

return true;
}
else{

return false;
}

}


public static void main(String [] args){
IntPalindrome obj=new IntPalindrome();
if(obj.isPalindrome(123213)){
System.out.println("The number is a palindrome.");
}else{
System.out.println("The number is not a palindrome.");

}

}

}

- badalrocks January 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

Badal actually its quite easy

public boolean checkPalindrome (int num)
{
int reverse =0;
while(num)
{
int quotient = num/10;
reverse = reverse * 10 + num %10;
num = quotient;
}
if (num == reverse)
{
return true;
}
else
{
return false;
}
}

- Abid January 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Abid: don't you need another var for num inside the while? Or the num in the following if statement will always be 0. right?

- roger September 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

to Abid:

wouldn't it be easier to first convert the number into a String and then reverse it and then check whether the reverse is equal? It might lower the chance of getting it wrong in your first try unless you're a math guru.

- Anonymous April 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

math guru? LOL... This is trivial math.

- LOL April 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In both the codes given above, there is no code to handle any kind of exception that might occur while check for pallindrome. What if we have a number like 12a21 ?

Badal's code will throw a NumberFormatException in that case which can be handled like
catch(NumberFormatException e){
e.printStackTrace();
return false;

}


while Abid's code will fail straightaway. So i think we must convert it first into a string before checking for pallindrome since we also have to check the exceptional cases.

Please tell me if I'm missing something here.

- AM May 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its pretty simple. Use the following python code to test for palindromes.

def isPalin(n):
    given = str(n)
    rev = given[::-1]
    if given == rev:
        return 1
    else:
        return 0

- reema August 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ispallindrom(int n)
{
int i=n;
int m=0;
while(i>0)
{
m=m*10+(i%10);
i/=10;
}
if(m==n)
return true;
else return false;
}

- veer October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We reverse the bits of the original number into a new number and test for equality. The reverse is done similar to reversal of a linked list, by removing the LSB from the first followed by a right shift, and adding to the LSB of the second followed by a left shift.

bool isPalindrome(int x)
{
	int n=x, m=0;
	while(n>0)
	{
		m = (m<<1) | (n&1);
		n = (n>>1);
	}
	return x==m;

}

Thanks!

- catalin4ever December 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice, now thats how geeks think

- geek January 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well that is what like a stupid thinks
totally wrong
reversing bits of a number doesnot reverses a number.. can't you just think for a while

- @geek February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

323 converted to base 2:101000011

389 converted to base 2:110000101

return 323 == 389

- M August 12, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More