Amazon Interview Question for Software Engineer / Developers






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

//asume the unsigned number.  
	unsigned int x = 1 ;
	unsigned int y=0, result=0;

	for (int i=0; i<32; i++)
	{
		y = x & 1;
		result |= y;
		if (i != 31)result = result << 1;
		x = x>>1;
	}

	cout << "result: "<<result<<endl;

- john June 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution given by John does a bytewise reversal of the int (byte=8 bits). Bytewise reversal would involve the reversal in positions of the entire bytes..right?

- Lord_of_the_Rings June 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey John..
I doubt the solution given by you to be bit wise reversal instead of the byte wise reversal..

- Anonymous June 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ya sorry, John's solution does a bitwise reversal.

- Lord_of_the_Rings June 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using reinterpret_cast convert int to char* then do a string reversal?

- Anonymous June 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This looks okay and serves the purposes.

- Anonymous June 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void byteRev(int num)
{
int a1=num&(0xF<<24);
int a2=num&(0xF<<16);
int a3=num&(0xf<<8);
int a4=num&0xF;

return (a1>>24)|(a2>>8)|(a3<<8)|(a4<<24);

}

- Jean June 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why
num&(0xF<<24);

why not num<<24?
wouldnt num<<24 give the number representing first 8bits?

I m not sure why do num&(0xF<<24);

Please help

- Anonymous March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//correct the return type
int byteRev(int num)
{
int a1=num&(0xF<<24);
int a2=num&(0xF<<16);
int a3=num&(0xf<<8);
int a4=num&0xF;

return (a1>>24)|(a2>>8)|(a3<<8)|(a4<<24);

}

- Jean June 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a "union" consisting of -
- 4 char variables (or just one array of 4 char) and
- one int. (I assumed one int is 4 bytes, unix style, and not 2 bytes, windows style)

Now store a value into union.int variable; access it 'backwards' using the 4 char variable and save back to int, like this -
int tmp = union.char[3]*1000 + union.char[2]*100 + union.char[1]*10 + union.char[0]
union.int = tmp;

print union.int.

Above syntax is of course symbolic and not strictly C.

Say what?

- Nik June 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 0, result = 0;
  int num; // user given number
  for(i=0;i<32;i++)
  {
    result = result << 1;
    if(num & (1<<i))
    {
        result = result | 1;
    }
  }

- Ashutosh June 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int reverse(int value)
{
   int i;
   int shift;
   for (i=0; i <= 15; i ++)
     if ((!(value & (1<<i))) != (!(value & (1<<(31-i)))))
     {
        shift = (value & (1<<i))?i:(31-i);
        value = (value & (~(1<<shift)))
        value = (value | (1<<(31-shift)));
     }
   return value;
}

- Mustafa June 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One more solution for 32 bit num with O(1) complexity.

int BitRev(unsigned int num)
{
    return ( ((num & (1 << 31))  >> 31)
            | ((num & (1 << 30)) >> 29)
            | ((num & (1 << 29)) >> 27)
            | ((num & (1 << 28)) >> 25)
            | ((num & (1 << 27)) >> 23)
            | ((num & (1 << 26)) >> 21)
            | ((num & (1 << 25)) >> 19)
            | ((num & (1 << 24)) >> 17)
            | ((num & (1 << 23)) >> 15)
            | ((num & (1 << 22)) >> 13)
            | ((num & (1 << 21)) >> 11)
            | ((num & (1 << 20)) >> 9)
            | ((num & (1 << 19)) >> 7)
            | ((num & (1 << 18)) >> 5)
            | ((num & (1 << 17)) >> 3)
            | ((num & (1 << 16)) >> 1)
            | ((num & (1 << 15)) << 1)
            | ((num & (1 << 14)) << 3)
            | ((num & (1 << 13)) << 5)
            | ((num & (1 << 12)) << 7)
            | ((num & (1 << 11)) << 9)
            | ((num & (1 << 10)) << 11)
            | ((num & (1 << 9))  << 13)
            | ((num & (1 << 8))  << 15)
            | ((num & (1 << 7))  << 17)
            | ((num & (1 << 6))  << 19)
            | ((num & (1 << 5))  << 21)
            | ((num & (1 << 4))  << 23)
            | ((num & (1 << 3))  << 25)
            | ((num & (1 << 2))  << 27)
            | ((num & (1 << 1))  << 29)
            | ((num & (1 << 0))  << 31)
           );
}

- Ashutosh June 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL.

- LOLer August 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL as well :_)

- Lei September 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done using recursion. Basically Merge Sort approach. Once you know the size of int you know how many bytes are there. Lets assume we have 32 bit int so 4 bytes. Divide in two parts lets say A and B. Move bits in A to right by size of A places and similarly move the bits in B to the left. Further divide A into 2 parts if possible and then again apply the same procedure. Now as you return start merging A and B by simply doing an OR. Little complex to write but I guess this could serve as a generic solution. Will try to post code sometime soon.

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

Anonymous' approach is correct. it does the job

- googler August 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code for the anonymous sol,

eg: 10010101 o/p => 10101001

	//initially S = (SizeOfTheInteger/2)
	//T = SizeOfTheInteger

	reverse(val , T , S)
	{
		leftVal = val >> S; //rightshift
		rightVal = val << (T-S); //left shift
		rightVal = val >> (T-S); //again right shift
		
		if(right shifting of leftVal once doensn't make leftVal == 0)
			leftVal = reverse(leftVal);
		if(left shifting of rightVal once doesn't make rightVal == 0)
			rightVal = reverse(rightVal);
		rightVal = rightVal<<S;
		return (leftVal | rightVal);
	}

- sindhu August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Small mistake in my prev post

...
leftVal = reverse(leftVal , T , S/2);
...
rightVal = reverse(rightVal , T , S/2);
...

- sindhu August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can have the termination condition as just S==1

- sindhu August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you all are right :)
but this is in words
1byte = 8 bits (genius me...)
eg. 1011 now what you all do to get last 2 bits ?? divide by 2^(bits you want)
here 4 ... got 10 as quotient 11 as remainder
now 11<<(bits you want ) | quotient == 1100|10 = 1110
EXPAND FOR 8 BITS

i=2^8;
j=8;
fine=0;
while(num > 0)
{
rem=num%i;
fine = fine<<j||rem
num=num/i;
}

- pull the bull August 22, 2009 | 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