Amazon Interview Question for Software Engineer / Developers






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

@Anonymous: it doesn't matter. Here is my solution.

boolean isPalindrome(int x)
{
    int y=0,z=x;
    while(z)
    {
        y = (y<<1)|(z&1);
        z=z>>1;
    }
    return (y==x)?true:false;
}

- Googler June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

superrrrrr... thanks dude...

- kumarasvn June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent.

- varun September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this won't work for negative numbers

- Anonymous December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please make sure to typecast the variables to unsigned integer and then do the right shift operations.....
If integer is right shifted, then it is arithmetic right shift.
If unsigned integer is right shifted then it is logical right shift.
This mistake I am seeing in almost all the posts of this forum....

- anonymous February 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we need to consider all bits in sizeof(int)??
Or only the number of digits required to represent that integer in binary notaion.
e.g. lets say sizeof int is 4 byte=32 bits
so 5 = 00000000000000000000000000000101
or we need to consider only 101

- Anonymous June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

zeros in higher order we can leave.Just consider the bits that are required to represent the numbers.

- kumarasvn June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not a efficient program, but works below code:
int n=0, num = 9;
int val = num;
while(num)
{
n = n << 1;
n |= (1&num);
num = num >> 1;
}
if(n == val)
cout << "Palindrome" << endl;
else
cout << "NOT Palindrome" << endl;

- Anonymous June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ispalindrome(int x)
{
        int n=x,y=0;
        while(n)
        {
                y=(y<<1) | (n&1);
                n = n>>1;
        }
        return x==y;
}
 
int main()
{
        int a=3;
        printf(ispalindrome(a)==1?"Palindrome.":"Not a palindrome.");
}

- ananthakrishnan.s.r June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

HI Guys You Can No One Has Given The Algorithm Here , so if anyone interested to analyze that ,i coded & explained it here some time back , you can have a look , comment if anything wrong
shashank7s dot blogspot dot com/2011/05/wap-to-check-that-binary-representation dot html

Shashank

- WgpShashank June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is an optimization for the case when the palindrome has an even number of bits.

boolean isPalindrome(int x)
{
   int z = x;
   int y = 0;
   while ((x != y) && (x!=0))
   {
      y = (y << 1) | (x & 1);
      x = x >> 1;
   }
   return x == y || z == y;
}

- Anonymous September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int palindrome(int x){
     int i ;
     for(i = 0; i < 32; i++){
         if( ( (x >> 31 - i) ^ ( x & (1 << i)) ) != 0)
             return 0;
     }
     return 1;
 }

- abc April 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

U need work on your bracketing and this won't directly work

- Anonymous April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define TRUE 1
#define FALSE 0

int is_palindrome(int x)
{
    int reverse = 0;
    int result;
    int y = abs(x);

    while (y != 0) {
        reverse = reverse << 1;
        if ((y & 1))
            reverse = reverse | 1;

        y = y >> 1;
    }

    result = reverse & abs(x);
    if (x < 0) result = 0 - result;

    return (x == result) ? TRUE : FALSE;
}

- Ashish19121 March 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Python
{
def palindrome(a):
b=a[::-1]
if (b==a): return True
else: return False
}
}

- Alex July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for bad code formatting

- Alex July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define isOne(num,pos)  (( num & ( 1 << pos ) ) ? TRUE : FALSE )
int isPalindrome( int num ) {
   for( pos = 15 ; pos != -1 ; pos-- )
      res = res + ( ( isOne(num,pos) ) ? ( 1 << (15 - pos) ) : 0 );
   return ( num == res );
}

- get2jawa July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will work for negative numbers too....

- get2jawa July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

here size of int is 2bytes

- get2jawa July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#define isOne(num,pos)  (( num & ( 1 << pos ) ) ? TRUE : FALSE )

int isPoli( int num ) {
   int pos;
   for( pos = 15 ; pos != 7 ; pos-- )
      if( isOne( num, pos ) != isOne( num, 15 - pos ) )   return FALSE;
   return TRUE;
}

- get2jawa July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works without having an extra argument....

- get2jawa July 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is another version......

#include<stdio.h>
void main()
{
unsigned int num = 12345;
unsigned char  rmb, lmb, ctr;
for(ctr=1; ctr<(sizeof(num) * 8)/2; ctr++) {
rmb = (num & (1<<((sizeof(num) * 8) - ctr ))) == 0 ? 0 : 1;
lmb = (num & (1<< (ctr-1))) > 0 ? 1 :0;
if(rmb!=lmb) break;
}
if(rmb==lmb) printf("Pali");
else printf("! Pali");
}

- get2jawa July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this logic

boolean is_palindrome(int x)
{
int comp=~x;
if( x&comp==0) return true ;
else false;
}

- Puneet July 23, 2013 | 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