Amazon Interview Question for Developer Program Engineers






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

Use sprintf function.
sprintf(buffer, "%f", f);
will print value of f into string buffer.

- AmitG November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is a good way , to use sprintf,
here is another way, without sprintf:
I haven't consider negative number , only decimal float are considerred .

void myftoa(float x)
{
	int i=x;
	int j;
	float m=x-(float)i;
	char CInt[18]={0}; // biggest integer decimal  would be 10 digital, we need another 1 for'.', 1 for 0
	for( j=0;j<10;j++)
	{
		if(i/10!=0)
		{
		CInt[10-j-1]=i%10+'0';
		i=i/10;
		}
		else
		{
			if(i%10!=0)
			{
            CInt[10-j-1]=i%10+'0';
			}
		break;
		}
	}
    char *result=&(CInt[10-j-1]); 
	char Cmas[7]={0}; // only have to handle value bigger than 0.000001
	for(int i=0;i<6;i++)
	{
     Cmas[i]=(int)(m*10)+'0';
	 m=m*10-(int)(m*10);
	}
	char *p=".";
    result=strcat(result,p);
    result=strcat(result,Cmas);
	printf("%s", result);

}

- Charles December 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

handle decimal point? isn't that the diff from itoa?

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

yeah..i wrote similar to itoa js to make d ques clear..

- @above November 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

any ideas??

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

1. Understand the IEEE floating point format.
Then use log10 function to determine the decimals in integer and fraction.
then you can print it.

- David December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use sprintf
or check this for implementing ftoa without using sprintf:

char*  floatToString(float num)
{
   int whole_part = num;
   int digit = 0, reminder =0;
   int log_value = log10(num), index = log_value;
   long wt =0;

   // String containg result
   char* str = new char[20];

   //Initilise stirng to zero
   memset(str, 0 ,20);

   //Extract the whole part from float num
   for(int  i = 1 ; i < log_value + 2 ; i++)
   {
       wt  =  pow(10.0,i);
       reminder = whole_part  %  wt;
       digit = (reminder - digit) / (wt/10);

       //Store digit in string
       str[index--] = digit + 48;              // ASCII value of digit  = digit + 48
       if (index == -1)
          break;    
   }

    index = log_value + 1;
    str[index] = '.';

   float fraction_part  = num - whole_part;
   float tmp1 = fraction_part,  tmp =0;

   //Extract the fraction part from  num
   for( int i= 1; i < PRECISION; i++)
   {
      wt =10; 
      tmp  = tmp1 * wt;
      digit = tmp;

      //Store digit in string
      str[++index] = digit +48;           // ASCII value of digit  = digit + 48
      tmp1 = tmp - digit;
   }    

   return str;
}

- Nitin February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

THis is not correct.

- Rocket Singh February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

float f;
int k,count=0,i=0;
char s[20];
scanf("%f",&f);
k=(int)f;
while(f-k!=0.0)
{
f=f*10;
k1=(int)f;
count++;
}

//now f is in integer format and now decimal can be placed using count
while(f>0)
{
if(count!=0)
{
s[i]=(f%10)+48;
f=f/10;
}
else
{
s[i]='.';
}
i++;
count--;
}
//reversing the above string gives the desired result....
awaiting u r responses :)

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

how to know how much memory should be allocated for string.

- Learn Android: http://learnandroideasily.blogspot.in/ June 15, 2011 | 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