Amazon Interview Question for Software Engineer in Tests






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

At first I thought its an infinite loop. But when I ran this I came to know that this prog will stop. These are the last two values of F1 and F2.
F1=16777216.000000, F2=16777215.000000
F1=16777216.000000, F2=16777216.000000
Can anybody explain y F1 didn't increase after 16777216.000000 ?

- Tulley February 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I got almost the same but 16777222 (C# VS2010).
Results depend on how float numbers stored in the memory, but I don't know how exactly explain this. For double type you'll get infinite loop.

- bill February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Floating point numbers are stored in base 2 "scientific notation". Once x gets large enough (about 2**24, since floats have 23 bits of significand), x is the closest float to the real number x + 1. The exact behavior depends on rounding modes and how floating point is implemented internally.

Doubles have the same problem, except you need to have the patience to get to about 2**53...

- Anonymous February 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do u mean the values achieved their max thats y they didn't increase??
as per IEEE the float representation is:
bit 31-->Sign bit
bit 30-23-->Exponent
bit 22-00--> Mantissa
In my case Exponent is 11000 and mantissa is 1. No overflow.

- Tulley February 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not the max; the exponent could be larger. However, with respect to that exponent, the
"ones" bit of the significand (formerly the mantissa) is one past the end and thus is thrown away.

- Anonymous February 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree -- the code should terminate when adding 1.0 can't change the mantissa of f1 (because the exponent has gotten "pretty large"). Overflowing f1 isn't required

- Peter May 20, 2011 | 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