Amazon Interview Question for Software Engineer / Developers


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

- Anonymous April 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- An April 05, 2012 | Flag
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

- vipul4group October 07, 2012 | Flag
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;
    }

- bitstuff April 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- RD April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Junk comment

- RD April 17, 2012 | Flag
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.

- Anonymous August 19, 2012 | Flag
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.

- Anonymous April 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great....

- Kiran April 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Kiran April 06, 2012 | Flag
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?

- Anonymous April 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Both your answers are correct

- An March 12, 2013 | Flag
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);
}

- sunny April 05, 2012 | Flag Reply
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.

- Selmeczy, P├ęter April 05, 2012 | Flag Reply
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;
		
	}

- vishal April 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Vladimir Dubovoy April 05, 2012 | Flag Reply
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)) ;
}

- Vijay Rajanna April 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what will happen when pos<0?

- anonymous April 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- An April 08, 2012 | Flag
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.

- Siddharth April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- An March 12, 2013 | Flag
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.

- jai April 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- asbelsare March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- get2jawa July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

another method...

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

- get2jawa July 22, 2013 | Flag Reply
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;
	}

- Anonymous May 01, 2015 | Flag Reply
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 }

- sandeepkumar211221 August 17, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More