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 In Test at Amazon.com:
Asked to implement a function that takes an integer and returns whether or not the number had an odd or even number of 1 bits.
| Tags: | technical, c, bit-masking See more , See less 8 |
See more for this Amazon.com Software Development Engineer In Test Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (6)
bool is_odd_set_bits(unsigned number)
{
bool result = false;
int n = number;
do
{
result |= ((n % 2) == 1);
n /= 2;
}
while ((n / 2) != 0);
return result;
}
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
// O(log2(n)) times
function evenOrodd($num)
{
$result=0;
while($num!=0)
{
if($num%2==1)
$result++;
$num = $num/2;
}
return ($result%2==1)?"odd" : "even";
}
?>
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
<<Modifying Tim's base code>>
bool is_odd_set_bits(unsigned number)
{
bool result = false;
int n = number;
while(n != 0)
{
result |= ((n & 0x01) == 1);
n >> 1;
}
return result;
}
masking is faster than a mod operator, and bit shifting is faster than divisions
Helpful Answer?
Yes |
No
Inappropriate?
I guess changing the line to:
result ^= ((n & 0x01) == 1);
will do the job...
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



1 of 1 people found this helpful
by Interview Candidate: