Amazon Interview Question: Determine whether the binary ... | Glassdoor

## Interview Question

Software Development Engineer II Interview Seattle, WA

# Determine whether the binary representation of a number if

a palindrome or not, code it on a white board.

0

This was the first question I was asked and is considered a warm up.

Interview Candidate on Mar 19, 2009

This post has been removed.

0

anon.. would this work for a number like 17 (10001)?

interview candidate on Jul 18, 2009
0

bool checkPalindrome(unsigned int n)
{
int m = n, k =0;
bool ret = false;
while(m!=0)
{
int i = 1;
i = i &amp; m;
k = k &gt; 1;
}
if((k^n)==0)
{
cout&lt;&lt;"Palindrome"&lt;

fedex on Aug 13, 2009
0

I have a simple solution. Reverse the bits one by one and test equality of the two resulting numbers. (Matlab code)

function [r] = isPalindrome(a)

n = a;
m = 0;

while(n&gt;0)
m = bitshift(m, 1);
m = m + mod(n, 2);
n = bitshift(n, -1);
end

r = (m==a);

end

catalin4ever on Nov 20, 2009
0

public static boolean isPalindrome(int n) {
int nb = 0, nl=n;
while(nl&gt;0) {
nb=(nb&gt;1;
}
return nb==n;
}

john on Jan 25, 2011
0

@john/catalin4ever,
i think it'll fail because it doesn't concat the 0s. For example: if the input is 0110,
then after execution "nb" becomes to 0011. this is because of the condition: while(nl&gt;0)

sathish on Jun 9, 2011
0

Ask interviewer if they want the MSB that is a 1 to dictate the bit range to check, have it given as a parameter, or assume sizeof(T)*8 perhaps. Little details and extras like this can make a difference to them.

public static bool CheckPalindrome(uint n)
{
return CheckPalindrome(n, 0);
}

unsafe public static bool CheckPalindrome(uint n, int bits)
{
if (bits == 0)
{
// Determine bits to check as the MSB having a 1
uint temp = n;
while (temp != 0)
{
++bits;
temp &gt;&gt;= 1;
}
}

uint m1 = (uint)1 &gt; 1; i &gt; 0; --i)
{
if (((n &amp; m1) != 0) ^ ((n &amp; m2) != 0))
{
return false;
}
m1 &gt;&gt;= 1;
m2 &lt;&lt;= 1;
}
return true;
}

Examples:

BitOps.CheckPalindrome(17, 0) is true
BitOps.CheckPalindrome(17, 8) is false
BitOps.CheckPalindrome(0xABD5, 0) is true
BitOps.CheckPalindrome(0xABD5, 16) is true
BitOps.CheckPalindrome(0x5BDA, 0) is false
BitOps.CheckPalindrome(0x5BDA, 16) is true

Ed on Apr 20, 2013
0

string binary;
int start=0,
int end= binary.length -1;

while(start &lt; end)
{
if (binary[start] == binary[end])
{
start ++; end- -;
}
else return false;
}
return true;

p on Mar 21, 2015
0

int isPalindrome( int num )
{
int x = num &amp; num;
return ( x == num ) ? 1 : 0;
}

Gary G on Jul 10, 2015
0

/* function declaration goes here.*/
int isPalin(int orig);

int main()
{
int x = 9;
int i = isPalin(x);
printf("i = %d \n", i);
}

int isPalin(int orig)
{
int copy = orig;
static int reversed = 0;

while(copy!=0)
{
reversed &gt;= 1;
}
return (reversed == orig);

Gary G on Jul 10, 2015
0

We can compare the leftmost and rightmost bits by left shifting and right shifting the bits and also by getting rid of those bits . If the binary number is a palindrome, the left and right bits should be equal.

Here is a C# implementation of the above logic

static bool IsBinaryPalindrome(byte num)
{
int i = 0;

while (true)
{
i++;
bool right = false, left = false;

byte origNo = num;

if (((num &gt; i) != origNo)
{
left = true; //left most bit contains one
}

num &gt;&gt;= i; //putting the right bit back in its original position

origNo = num;
if (((num &gt;&gt;= i) &lt;&lt; i) != origNo)
{
right = true; // right most bit contains one
}
num &lt;&lt;= i; //putting the left bit back in its original position

if (left != right)
{
return false;
}

if (num == 0 || num == 1) break;
}
return true;
}

Kumar R on Apr 4, 2016
0

python one-line need to replace the the 0b

&gt;&gt;&gt; str(bin(255).replace('0b',''))==str(bin(255).replace('0b',''))[::-1]
True

Anonymous on Jun 15, 2016