Amazon Interview Question for Software Engineer / Developers






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

Try to find the last occurrence of the pattern "01" in the binary representation of the number.
Set that 01 to 10, and put all 1's after that position to the end.
e.g.
n=10111 01110
the last occurrence of 01 is of index 6.
the algorithm gives m=10111 10011.

- ftfish July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good approach

- xclamation July 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the input is 1100? exp ans: 10001

- Kos January 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Count the number of Bits that are set in the given number
This can be done in O(no of bits that are set) time
Now the number can be from left to right all 1 set and then 0

Eg
110010
Bits set : 3
new number 111000

- DashDash July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ftfish's approach seems correct.
@ibnipun10: your ans is wrong. it'd be 110100.

- buried.shopno July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@buried: Can you please explain as to why the ans is wrong?

- DashDash July 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is because you don't need to find just any greater number than the given number. You need to find the number which is greater but the diff between the numbers is minimum.

- endeav.rd July 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You din't say why ibnipun10's answer is wrong.
You just changed the question to disprove his answer.

- Mahesh October 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey18842" class="run-this">class NextBinary {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int k = 12;
System.out.printf("Input Number:%2d", k);
System.out.printf(" Binary Format:%3s", Integer.toString(k, 2));
System.out.println();
int n = nextBinary(k);
System.out.print("Output Number:" + n);
System.out.printf(" Binary Format:%3s", Integer.toString(n, 2));
}

private static int nextBinary(int z) {
// TODO Auto-generated method stub
int i = 0;
int ones = 0;
while ((z & 1 << i) == 0)
i++;
z = z & ~(1 << i);
i++;
while ((z & 1 << i) > 0) {
i++;
ones++;
}
z = z | (1 << i);
i--;
while (i >= ones) {
z = z & ~(1 << i);
i--;
}
while (i >= 0) {
z = z | (1 << i);
i--;
}
return z;

}

}
</pre><pre title="CodeMonkey18842" input="yes">1
2
10
42
11

</pre>

- Anonymous July 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can't we just multiply the number by 2 ???

- AnonK July 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes. multiplying by 2 preserves the number of 1's. But there is a possibility that the new number goes out of bounds of the datatype.

- bluegene July 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int nextGreaterNumWithSameNumOfOnes(unsigned int num)
{
unsigned int numOfZeros = 0 , numOfOnes = 0;

if ( num == 0)
return 0;

// count num of zeros
while(num%2 == 0)
{
numOfZeros++;
num = num>>1;
printf("num of 0's %d \n",numOfZeros);
}

//count num of 1's
while(num && (num%2 == 1))
{
numOfOnes++;
num = num>>1;
printf("num of 1's %d \n",numOfOnes);

}

//flip the bit .. LSB's 0
num = num + 1;

//shift 1
num = num << 1; // move zero into LSB
numOfOnes--; // remove 1 bit from original binary form

while(numOfOnes)
{
num = num << 1;
num = num+1;
numOfOnes--;
}

while(numOfZeros)
{
num = num<<1;
numOfZeros--;
}

printf("next greater num is %d \n",num);

return num;

}

- krish July 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return n*2

- Anonymous November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

multiplying with 2 is incorrect:
for n=6 i.e 110 n*2= 12 i.e.. 1100
but ans should be 1001

ftfish is correct:
find last occurence of "01" replace it with "10" then move all 1' to the end.

- Anonymous June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

multiplying with 2 is incorrect:
for n=6 i.e 110 n*2= 12 i.e.. 1100
but ans should be 1001

ftfish is correct:
find last occurence of "01" replace it with "10" then move all 1' to the end.

- Varun June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ftfish is correct.. there are two corner cases.

a) if all bits are one then no solution. - obvious
b) or if there is no occurrence of 01 and all zeros are trailing zeros like 111000 then answer should be 1100001

- Anonymous October 19, 2011 | Flag


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