## Amazon Interview Question for Software Engineer / Developers

• 2

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
3
of 5 vote

k = 1 << pos
return ((number & k) > 0)

number & k may not be 1

number = 100
pos = 3
100 & 100 = 100 (4) not 1

Comment hidden because of low score. Click to expand.
0

Yes i figured this out eventually after the interview when i sat and wrote all the steps.

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

I think it should be

``k = 1 << (pos-1)``

1 << 3 // will be 1000 i.e. fourth bit

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

You want to check that "number AND k" preserves k

``````boolean isSet1(int number, int pos) {
int k = 1;
for(int i = 1; i <= pos; i++) {
k = k << 1;
}
return (number & k) == k;
}``````

Comment hidden because of low score. Click to expand.
0

That would require number to be a power of 2. If you '&k', it will erase the other bits of number except for the one at pos.

Comment hidden because of low score. Click to expand.
0

Junk comment

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

FOR loop should be like:
"for(int i = 1; i < pos; i++)"

Please correct me if I am wrong.

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

One way is :

``````public static boolean isBitSet(int n, int pos) {
return ((n & (1 << pos)) > 0);
}``````

another way is

``````public static boolean isBitSet(int n, int pos) {
return (((n >> pos) & 1) == 1);
}``````

bit position start at 0.

Comment hidden because of low score. Click to expand.
0

Great....

Comment hidden because of low score. Click to expand.
0

Shall i see how the first method works... ?? i can't understand ..

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

One example is the number 15 and we want to know if the bit in the position 3 is on, so the binary number for 15 is 00001111, so what the method does is turn the bit in the given position creating this number 00001000 then it does an AND of 00001111 & 00001000 which should give us the same number 00001000 as a result. If the bit asked were 0 and let's say the given number was 7 instead, which is 00000111, then when doing 00000111 & 00001000 the result would be 0, makes sense?

Comment hidden because of low score. Click to expand.
0

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

Another way could be:
bool is_set(int number, int pos){
return ((bool)(number>>pos)&0x1);
}

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

(number & k) > 0 should read as (number & k) != 0 [or simple (number & k) if in C] in all the codes above.

If the shifting puts the bit to the topmost bit in k than the result will be negative, but still non-zero.

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

``````static boolean findbit(int no,int pos)
{
int k=1<<pos;
System.out.println(k);
if((k&no)==k)
return true;
else
return false;

}``````

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

boolean bitset(int no,int pos)
{
return (no & (1 << bit)) != 0;
}

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

You need to consider whether the bits are counted from 0 or 1.
The above approaches wroks if the bits are counted from 0, but if counted from 1, below is the solution.
bool bitset(int number, int pos)
{
return (number & pow(2,(pos-1)) ;
}

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

what will happen when pos<0?

Comment hidden because of low score. Click to expand.
0

I dont know...did not ask him that, what should happen, throw an error? In that case its just an if check....

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

probably you can just divide num by 2 for position number of times and then take num % 2.
if the result is 1 then return true else return false.

Comment hidden because of low score. Click to expand.
0

It was the first time i attempted solving a bit manipulation question. Honestly it does not matter whether you loop or not, and I don't think that is what the interviewer judged me on.

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

y u use the loop u jst shift the number by (pos -1) unit to right and and with 1.u get the result.

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

return (n>>p & 1) ? true : false;

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

``return ((1<<pos) & num )>>pos;``

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

return ( num & pow( 2 , pos ) ) ? true : false

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

another method...

``return ( num & (1 << pos) ) ? true : false;``

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

Using XOR:

``````if(number ^ (1 << (pos - 1))){
return true;
}
else{
return false;
}``````

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

``````1 #include<stdio.h>
2
3 int bitSet(int num,int pos)
4
5 {
6
7     if((num &(1<<(pos-1))))
8
9         return 1;
10
11     else
12         return 0;
13
14 }
15
16
17 int main()
18
19
20 {
21
22     int num,pos;
23
24     printf("Enter the Number ");
25
26     scanf("%d",&num);
27
28     printf("Enter the position :");
29
30     scanf("%d",&pos);
31
32     if(bitSet(num,pos))
33
34         printf("True");
35
36     else
37         printf("False");
38
39
40
41 }``````

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.

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