Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

class Float2String
{
public static void main(String[] args)
{
float f=7.64f;
String result;
result=""+f;
System.out.println(result);
}
}

- karthik dheeraj September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good ans dude

- nuhaque September 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what in the worlde. LMAO

- anon February 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

int i;
i = (int) f;
//convert this integer into string ... and put a decimal ...
.
.
f = f - i; //now f contains the fractional part ...
f *=10;
i = (int) f; //this is first digit to the right of decimal ...
.
.
f = f-i;
f *= 10;
i = (int) f; // this is second digit to the right of decimal...
// repeate above steps four more times ...

- EOF September 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution based on higher level operations/abstractions like casting. +1. A little more formal treatment is below.

int i, j = 0;
i = (int) f;

f = f - i;
while(f > 0) {
  f *= 10;
  j = (j*10) + (int) f;
  f = f - (int) f;
}
println  atoi(i) + "." + atoi(j)

It might turn out to be a tough problem if casting is disallowed.

- Murali Mohan September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, please read atoi() as itoa() in the above comment.

- Murali Mohan September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Erasmus : 'itoa()' is a built in function

- Anonymous September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous

The question is asking to focus you on a procedure for conversion and the conversion should not use a library function. However, in order to convert a number into a string of char literals, you ought to have an itoa() function, which you can always develop on your own.

- Murali Mohan September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there could be a problem :
suppose f=53.64
when i write code :
j is index after decimal part
while(rational>0)
{

int integer=(int)rational;
rational=rational-integer;
strin[j]=48+integer;
cout<<"thave value in decimal "<<j<<" is"<<strin[j]<<endl;
j++;
rational=rational*10;
}

i expect sollution to be 53.64
but actual result is :53.639999.....( where ... are digits till end of array) .
Now the question is how to round off 53.63999 to 53.64.. (Even the interview wants this too i guess, which is how float is represented in language )

- Hemanth Muttevi September 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Hemanth Muttevi: Just do this and you will know where the problem lies.

float test=7.646
printf("%f\n", test);

In my system it is printing 6.459999 so....

- aka September 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is use of overloaded operator allowed ? if so in Java

String result = "" + <number value i.e 7.64>;
but it can not be this simple :)

- varun.sudan September 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

As per Question I got, Your answer would be best in this case.

- Jayesh September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I solved it in c++...Please let me know any improvements..

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>

#define MAXLEN 350

using namespace std;

void reverse(char * str , int len){
    int i=0,j=len-1;

    char tmp;

    while(i<j){
        tmp=str[j];
        str[j]=str[i];
        str[i]=tmp;
        j--;i++;
    }

}

char * floatToString(double f ,char * str, int fractionPartSize)
{
    ofstream fout1 ("float1.out");

    int sign=f<0?1:0;
    int pos =0;
    int intPart = (int)f;
    double fractionPart= f-intPart;

    //one char at a time , first int part then fraction part

    if(intPart==0)
    {
        str[pos++]='0';
    }
    else if(sign==1)
    {
        str[pos++]='-';
        intPart=-intPart;
    }
        while(intPart>0){
            str[pos++]='0'+intPart%10;
            intPart/=10;
        }

        int intPartSize=pos-sign;
        reverse(str+sign,intPartSize);
        //int i=0;
        str[pos++]='.';
        while(pos<fractionPartSize+intPartSize+1){
            f=f-(int)f;
            f*=10;
            str[pos++]='0'+(int)f;
        }
         str[pos]='\0';
         fout1<<"string"<<str;


}

int main()
{
    ifstream fin ("float.in");
    ofstream fout("float.out");

    char mainStr[MAXLEN];

    double f;

    fin>>f;

    int fractionPartSize=6;

    floatToString(f,mainStr,fractionPartSize);

    return 0;

}

- Atul Mishra September 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't compile.
Errors in lines:

int sign=f<;0?1:0;

and

while(intPart>;0){ ...

- JRB September 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

just remove the semi colons..I don't know how it got there..I copied it directly from my local copy and it's not there. Thanks for pointing out.

- Atul Mishra September 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<string.h>

string reverse (string src)
{
int i = 0, j = src.length() - 1;
while(i < j)
{
char temp = src.at(i);
src.at(i) = src.at(j);
src.at(j) = temp;
i++;
j--;
}
return src;
}

string intToString(int src)
{
string result;
do
{
int res = (int)'0' + src%10;
src = src/10;
result.push_back(res);
}
while(src != 0);
return reverse(result);
}


int main(int argc, char* argv[])
{
string str;
float f = 7.65;
int intPart = (int)f;

int decPart = 0;
float temp = f - int(f);
while(temp - (int)temp != 0.0)
{
temp = temp*10;
}
decPart = temp;

str = intToString(intPart)+ '.'+ intToString(decPart);
cout << str;
cin.get();
return 0;
}

- Vova September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

float x = 7.98;
	char a[5] ;
	for (int i =0; i<4; i++){
		int m = (int)x;
		if(i==1)
			a[i] = '.';
		else 
			a[i] = m+48;
		x = x-m;
		x = x*10;
	}

	a[4] = '\0';
	string str = a;
	cout<<str<<endl;

- Solx September 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You don't know whether the program is using ascii.

- CrazyTed September 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
main()
{
  float x = 7.98;
  char a[5] ;
  int i; 
	for (i =0; i<=4; i++){
		int m = (int)x;
		if(i==1)
			{a[i] = '.';a[i+1]=m+48;i++;}
		else 
			a[i] = m+48;
		x = x-m;
		x = x*10;
	}

	a[4] = '\0';
	
        puts(a);
}

- dev.royiit September 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int convert_f_t_s(float num, int decimal_num)
{
int i = (int) num;
char *str = NULL;
float decimal = num - i;
int i_num = 0;
int tmp = 0;
int j = 0;
while (i) {
i = i / 10;
i_num++;
}

str = (char *)malloc(i_num + decimal_num + 2);
memset (str, 0, i_num + decimal_num + 2);
i = (int) num;
decimal = num - i;
for (j = 0; j < i_num; j++) {
tmp = i % 10;
i = i / 10;
str[i_num - j -1] = '0' + tmp;
}
str[i_num] = '.';
for (j = 0; j < decimal_num; j++) {
decimal = decimal * 10;
tmp = ((int)decimal % 10);

str[i_num + j + 1] = '0' + tmp;
}

printf("orl str is %s\n", str);
}
int main(int argc, char **argv)
{
float num = 34.234;

convert_f_t_s(num, 3);
}

- Vincent September 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess questions means not using anything to convert from number to characters.
And thus the answer should be independent of the language used.
My solution : use a trie containing 0-9. and then traverse in the trie with different digits from the number as input.

- Atul Jangra September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In JAVA

public static void main(){

float val = 7.6f ;
String str = " "+val;  //  This same statement can also be use in C# to convert float variable    	//into string
System.out.print(str);
}

- Pankaj Jawale September 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What they basically asked for is the implementation of itoa function.
Not the use of it to get the work done.

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

stringstream ss;
    string s;
    double f=1.94;
    ss<<f;
    ss>>s;
    cout<<s;

- Dima July 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

geekyjumps.blogspot.in/2013/09/given-float-number-convert-it-into.html

- Anonymous September 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Which part of "not using inbuilt function or library" you did not understand?

- confused_banda September 05, 2013 | 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