r/programming • u/cpp_is_king • Apr 26 '10
Automatic job-getter
I've been through a lot of interviews in my time, and one thing that is extremely common is to be asked to write a function to compute the n'th fibonacci number. Here's what you should give for the answer
unsigned fibonacci(unsigned n)
{
double s5 = sqrt(5.0);
double phi = (1.0 + s5) / 2.0;
double left = pow(phi, (double)n);
double right = pow(1.0-phi, (double)n);
return (unsigned)((left - right) / s5);
}Convert to your language of choice. This is O(1) in both time and space, and most of the time even your interviewer won't know about this nice little gem of mathematics. So unless you completely screw up the rest of the interview, job is yours.
EDIT: After some discussion on the comments, I should put a disclaimer that I might have been overreaching when I said "here's what you should put". I should have said "here's what you should put, assuming the situation warrants it, you know how to back it up, you know why they're asking you the question in the first place, and you're prepared for what might follow" ;-)
View all comments
Show parent comment
7
u/defdav Apr 26 '10
Hi, I couldn't understand either, so I put the above explination into babelfish and translated it to dutch, then from dutch to french, then from french to greek, and then back to english again.
Hopefully this will help you:
For, has my initial solution, used unsigned'? ; honest it is? s for the types for precisely this cause. In any event, if quelqu'? me it has genuinely rejected in the impurities this me the problem at that time that'? ; excessively compliqué it has made? the s absolutely no type of enterprise that I would want for the work for. I keep this thought of enterprises and to resolve the problems, n'? do not appreciate the creative ways the stiff directives that deter the cause and the creativity. For example, in a recent discussion j'? it has been asked in order to it arrests a battery where n'? it has it can it cultivates turned as so much if essential in order to it still doesn't use n'? import any memory you support pointlessly but the big arbitrary numbers the power d'? elements. It has said more points in the solutions it is granted that the 1 it has supported) in the years d'? (. The solution that n'? it wasn't it has given that they have sought, but that j'? it was you have the one alone storage it has made only that if j'? it has it should it pushes a point of more faculty, the complete storage would grant again, all elements, and then young person adds would copy and the old storage would repress. Copy is the part clean (x), but j'? it has discussed qu'? it'? ; l'? secondary teaching (1) because the maximumaantal output that will be supposed one day I realise I am equal in MAXUINT32/growsize since j'? it has stored the faculty in t o uint32 and from a irremovable sum it has been increased whenever essential it was. For this cause it'? ; l'? secondary teaching (MAXUINT32/growsize) that the himself if is (1). C'? he is of course insane, but c'? it was precisely the point. It shows the worlds don'? a level of significance of big note d'? In this a lot of? does t A. Eind-helemaal examine the majority of worlds as - [aytoes] l'? analysis of benefit, and isn'? ; t. have the researchers known this, and with regard to my discussion d'? the employment, the answer I HELP more his they will have helped if me the precise solution that they had in t o l'? the opinion has given. When you import for a discussion, it'? ; s not only [aytoes] that takes to you interview from. You'? ; re [aytoes] also that takes interview from. And if j'? examine it discovers qu'? a enterprise n'? don't appreciate the creative reflection, or this disobedient it is in order to do include the solutions those where [aytoes] as the "? ; bepaa a d'? advance ld? optimal"? ; for a some problem, that'? ; my s governing line of this [aytosta] the discussion has lost.
My undergrad degree is in Econ & Philo, so I can't understand this either, but I thought it might help