Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

Suppose that int is 32 bit (if not choose an integer-type that is)

1.

#include <stdio.h>

char *to_hex(int x) {
    static char buffer[9];
    sprintf(buffer, "%08X", x);
    return buffer;
}

int main() {
    int x = 0x1234ABCD;
    printf("ToHEX: %s\n", to_hex(x));
    return 0;
}

2.

include <stdio.h>

char *to_hex(int x) {
    static char digits[] = "0123456789ABCDEF";
    static char buffer[9];
    int i;
    for (i=0; i<8; i++) {
        buffer[7-i] = digits[x & 0xF];
        x >>= 4;
    }
    return buffer;
}

int main() {
    int x = 0xF1234ABC;
    printf("ToHEX: %s\n", to_hex(x));
    return 0;
}

- Selmeczy, Péter December 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{
int x;
scanf("%d",&x);
printf("%x",x);
return 0;
}

- Wayne December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wayne, i think conversion doesnt mean to print as hex, but to give a logic to actually convert :)

- abhityagi85 December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hehe... then we should start dividing the number by 16 :P

- Wayne December 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static String toHex(int n) {
		int mask = 15;
		StringBuffer sb = new StringBuffer();
		while (n != 0) {
			int index = (n & mask);
			char c = (char) ((index < 10) ? (index + '0') : ('A' + index - 10));
			sb.append(c);
			n = n >>> 4;
		}
		sb.reverse();
		return sb.toString();
	}

- kartikaditya December 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how int n hold 32 digit integer?

- nmc December 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Absolutely.
Even unsigned long long would not be enough to hold this value. So unless you are implementing it in a scripting language like Ruby. you would need to implement division by 16 by representing the long number as string which is pain !

- Harish January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I will ask the interviewer, if the integer is a big integer (4 bytes int can't express it)

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

is there anything wrong with this code(assuming that its 32 bit integer that has to be converted to hexadecimal)
logic: each set of four bits corresponds to a hexadecimal digit, thus the four rightmost bit are collected through bit manipulation and converted to hexadecimal using lookup table.

char[] convert(int n){
mask= ~((~0)<<4);//makes rightmost 4 bits 1
char hex[8];//to store 8 digit hexadecimal
int digit=7;
while(n!=0){
int hex_bit=0;
hex_bit=mask&&no;//stores 4 righmost bits in hex_bit
hex[digit--]=convert_to_hex(hex_bit)//this function implements just a look up from
// 0 to 16 to convert decimal to hexa
n=n>>4;
}
while(digit>-1) hex[digit--]='0';//rest left most hexa digits are set to zero
}

- d_coder January 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is there anything wrong with this code(assuming that its 32 bit integer that has to be converted to hexadecimal)
logic: each set of four bits corresponds to a hexadecimal digit, thus the four rightmost bit are collected through bit manipulation and converted to hexadecimal using lookup table.

char[] convert(int n){
mask= ~((~0)<<4);//makes rightmost 4 bits 1
char hex[8];//to store 8 digit hexadecimal
int digit=7;
while(n!=0){
int hex_bit=0;
hex_bit=mask&&no;//stores 4 righmost bits in hex_bit
hex[digit--]=convert_to_hex(hex_bit)//this function implements just a look up from
// 0 to 16 to convert decimal to hexa
n=n>>4;
}
while(digit>-1) hex[digit--]='0';//rest left most hexa digits are set to zero
return hex;
}

- d_coder January 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is there anything wrong with this code(assuming that its 32 bit integer that has to be converted to hexadecimal)
logic: each set of four bits corresponds to a hexadecimal digit, thus the four rightmost bit are collected through bit manipulation and converted to hexadecimal using lookup table.

char[] convert(int n){
mask= ~((~0)<<4);//makes rightmost 4 bits 1
char hex[8];//to store 8 digit hexadecimal
int digit=7;
while(n!=0){
int hex_bit=0;
hex_bit=mask&&no;//stores 4 righmost bits in hex_bit
hex[digit--]=convert_to_hex(hex_bit)//this function implements just a look up from
// 0 to 16 to convert decimal to hexa
n=n>>4;
}
while(digit>-1) hex[digit--]='0';//rest left most hexa digits are set to zero
return hex;
}

- d_coder January 23, 2012 | 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