Amazon.com Interview Question
1,566 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer II at Amazon.com:
Determine whether the binary representation of a number if a palindrome or not, code it on a white board.
See more for this Amazon.com Software Development Engineer II Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (7)
0 of 1 people found this helpful
final int WIDTH = 8;
boolean isPalindrome = true;
for (int i = 0; i < WIDTH && isPalindrome == true; i++) {
int maskLower = (int) Math.pow(2, i);
int maskUpper = (int) Math.pow(2, WIDTH - (i+1));
boolean bitLowerOn = ((maskLower & someInt) == maskLower) ? true : false;
boolean bitUpperOn = ((maskUpper & someInt) == maskUpper) ? true : false;
isPalindrome = bitLowerOn && bitUpperOn && isPalindrome || !bitLowerOn && !bitUpperOn;
}
return isPalindrome;
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
{
int m = n, k =0;
bool ret = false;
while(m!=0)
{
int i = 1;
i = i & m;
k = k << 1;
k = k | i;
m = m >> 1;
}
if((k^n)==0)
{
cout<<"Palindrome"<<endl;
ret = true;
}
else
{
cout<<"Not Palindrome"<<endl;
}
return ret;
}
Helpful Answer?
Yes |
No
Inappropriate?
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
Helpful Answer?
Yes |
No
Inappropriate?
int nb = 0, nl=n;
while(nl>0) {
nb=(nb<<1) + (nl&1);
nl=nl>>1;
}
return nb==n;
}
Helpful Answer?
Yes |
No
Inappropriate?
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)
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: