Amazon Interview Question for Software Engineer / Developers






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

static StringBuffer intToString(int n){
    StringBuffer buf = new StringBuffer();
    
    if(n == 0){
        buf.append(0);
    }else{
        while(n > 0){
            buf.insert(0,n%10 );
            n/=10;
        }
    }
    return buf;
}

static String floatToString(double n){
    boolean nLessThanOne = (n < 1.0);
    if(nLessThanOne)
        n+=1;
    
    double floatPt = n;

    while(floatPt%10 > 0){
        floatPt*=10;
    }
    floatPt/=10;

    StringBuffer buf = intToString((int)floatPt);
    int floatPos = (int)floor(log10(n));

    if(nLessThanOne){
        buf.deleteCharAt(0);
        buf.insert(0, ".");
    }else{
        buf.insert(floatPos+1, ".");
    }

    return buf.toString();
}

- snowbeard January 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The answer is wrong you cannot do comparison on floating point operators and % cannot be applied to floating point numbers

- desi grad January 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The most obvious feature missing from the function is that it does not handle negative numbers but including that is trivial.

- snowbeard January 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this was telephonic or onsite interview question? US/India?

- Anonymous January 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Codind Round in India

- Prabhu M January 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

or else we simply can do this

String floattoString(float x)
{
  String y = ""+x;
  return y;

}

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

long beforeDP = d/1;
double afterDP = d - beforeDP;
StringBuffer sb = new StringBuffer();
if(beforeDP < 1) sb.append(0);
while(beforeDP > 1)
{
   long l = beforeDP % 10;
   beforeDP /= 10;
   sb.append(l);
}
sb.append('.');

long l = afterDP * 10;
while(l != 0)
{
  sb.append(l);
  afterDP *= 10.0;
  afterDP -= l;
  l = afterDP * 10;
}
System.out.println(sb);

- Voila January 22, 2010 | 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