Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Let's take 13 as an example, its binary format is:
00000000, 00000000, 00000000, 00001101, this is also the representation of little endian. The big endian format is 00001101, 00000000, 00000000, 00000000. You have to reverse the bytes, not the bits.

public class Solution{
    
    public int convertToBigEndian(int num){
        //we assume that it is a 32-bit number
        byte[] bytes = new byte[4];
        for(int i = 0; i < 4; ++i){
            bytes[i] = (byte)(num % 256);
            num = num>> 8;
        }
        int result = 0;
        for(int i = 0; i < 4; ++i){
            result = result << 8;
            result += bytes[i];
        }
        return result;
    }


    public String convertToBinary(int input, boolean littleEndian){
        if(!littleEndian){
            input = convertToBigEndian(input);
        }
        StringBuilder sb = new StringBuilder();
        while(input > 0){
            sb.insert(0, String.valueOf(input % 2));
            input /= 2;
        }
        if(littleEndian){
        	while(sb.length() < 32){
        		sb.insert(0, "0");
        	}
        }
        return sb.toString();
    }
    
    public static void main(String[] args){
    	Solution solution = new Solution();
    	System.out.println(solution.convertToBinary(13, false));
    }
}

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

If I understand it right, big and little endian should only affect the way they are stored in memory address and not their binary code representations.

otherwise a program containing a bitwise shift operator would not be portable since the meaning would change e.g. if lets say a represents 13 then your intention in a >> 1 would be to push bits in a to right by 1 position and you would expect 6 as the result. if a is binary 00000000, 00000000, 00000000, 00001101 then you would get what you expect instead if a is coded as 00001101, 00000000, 00000000, 00000000 then a >> 1 would give a different number altogether making the operation non-portable.

- Sandeep June 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What I am writing here is how the number is stored in memory. The compiler can do that conversion for us automatically, so we don't need to care about if the machine is using big endian or small endian when we are programming. For example, if you use GDB to debug c or c++ programs, gdb will do that conversion for you.

- ravio June 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok if compiler or some other layer beneath our program abstracts the big-endian vs little endian format for the program thus ensuring that the program works consistently across different systems then this program need not try to change the input to big-endian. Isnt it?

- Sandeep June 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it just aims to check if we know how a number is stored in big endian and little endian machine or system. As you know that almost all of our personal computers are using little endian and the internet protocol is using big endian. So the network adaptor has to do this conversion for us. Anyway, I just treat it as a chance to review endianness.

- ravio June 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actual intention of this problem is how the machine represents big endian and little endian.
The byte orders we have to concentrate as mentioned in the first comment.
Based on input parameter format( big or little endian ) value, we should print the binary numbers.

example: input 13 , big endian
big endian : 00001101 00000000 00000000 00000000

- Srinivas June 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lol

- Anonymous July 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
}

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

{
}

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

{
}

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

Here is short and sweet function

void binnum(int t)
{
unsigned int mask = 1 << (sizeof(int) * 8 - 1);

for(int i = 0; i < sizeof(int) * 8; i++)
{
if( (t & mask) == 0 )
cout << '0' ;
else
cout << '1' ;

mask >>= 1;
}
cout << endl ;

}

- keyurpatel80 July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static byte[] reverseArray(byte[] b)
{
byte[] newArr = new byte[b.length];
for (int i = 0; i < b.length; i++)
{
newArr[newArr.length - i - 1] = b[i];
}
return newArr;
}

public static StringBuffer getBinary(int num, boolean isBigEradian)
{
byte[] bytes = new byte[4]; //assuming 32 but int

int index = -1;

while (num != 0)
{
bytes[++index] = (byte) (num & 255);
num = num >> 8;
}

if (isBigEradian)
bytes = reverseArray(bytes);

StringBuffer sb = new StringBuffer();
for (byte b : bytes)
if (b != 0)
sb.append(convertByteToString(b));

return sb;

}

private static StringBuffer convertByteToString(byte b)
{
StringBuffer sb = new StringBuffer();
int count = 0;
while (b != 0)
{
sb.insert(0, b & 1);
b = (byte) (b >> 1);
count++;
}

while (count < 8)
{
sb.insert(0, "0");
count++;
}

return sb;
}

public static void main(String args[])
{
System.out.println(getBinary(786, false));
}

- Anonymous July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class DecimalToBinaryConverter {
    public String decimalToBinary(int num, boolean bigEndian) {
        StringBuilder strBr = new StringBuilder();

        while (num > 0) {
            strBr = bigEndian ? strBr.insert(0, num % 2) : strBr.append(num % 2);
            num = num / 2;
        }

        return strBr.toString();
    }

    public static void main(String[] args){
        DecimalToBinaryConverter convrtr = new DecimalToBinaryConverter();

        System.out.println(convrtr.decimalToBinary(11, true));
        System.out.println(convrtr.decimalToBinary(11, false));

    }

}

results are

1011
1101

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

You answer is wrong because you miss understood the concept of little endian and big endian. Let's take 13 as an example, its binary format is:
00000000, 00000000, 00000000, 00001101, this is also the representation of little endian. The big endian format is 00001101, 00000000, 00000000, 00000000. You have to reverse the bytes, not the bits. That's where you got the answer wrong. Please refer to my code for the corrent answer.

- ravio June 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The assumption that I am making here is that the output has to be presented in a big or little endian formatted binary number and not that the input is big/little endian formatted.

If I understand it right, big and little endian should only affect the way they are stored in memory address and not their binary code representations. Otherwise I guess programs containing shiftwise operators would not be portable.

The logic in my above code is still flawed though since as you pointed out the format is not applicable at bits instead it is at bytes.

- Sandeep June 07, 2014 | 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