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.
Answer

Interview Answer

13 Answers

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.
Please see our Community Guidelines or Terms of Service for more information.

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 & m;
        k = k > 1;
    }
    if((k^n)==0)
    {
        cout<<"Palindrome"<

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>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>0) {
            nb=(nb>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>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 >>= 1;
                }
            }

            uint m1 = (uint)1 > 1; i > 0; --i)
            {
                if (((n & m1) != 0) ^ ((n & m2) != 0))
                {
                    return false;
                }
                m1 >>= 1;
                m2 <<= 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 < 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 & 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 >= 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 > i) != origNo)
                {
                    left = true; //left most bit contains one
                }

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

                origNo = num;
                if (((num >>= i) << i) != origNo)
                {
                    right = true; // right most bit contains one
                }
                num <<= 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

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

Anonymous on Jun 15, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.