Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int fib(n) {
 if(n == 0)
   return 0
 if(n == 1)
   return 1
 return fib(n-1)+fib(n-2) }

Simulate fib(5)
This was my interview question. I don't know what the real answer is ! Can you help me by answering this question ?
Posted
Updated 29-Mar-11 10:34am
v2
Comments
Sergey Alexandrovich Kryukov 29-Mar-11 16:38pm    
Nothing is "simulated" here. This is just a regular recursive functions with two bugs. Please see my Answer for a fix.
--SA
Sergey Alexandrovich Kryukov 29-Mar-11 16:39pm    
Or, I see you fixed "=" to "==".
Now, fix (n-2) and it will work.
--SA
Sergey Alexandrovich Kryukov 29-Mar-11 16:40pm    
Already fixed. Now it should work.
--SA
Sergey Alexandrovich Kryukov 29-Mar-11 16:42pm    
No, not fixed yet. Also, add ";" after each statement.
--SA

fib(5) =                 fib(4)                +              fib(3)
           fib(3)          +       fib(2)            fib(2)    +    fib(1)
   fib(2)    +    fib(1)      fib(1)  +  fib(0)  fib(1) + fib(0)
fib(1) + fib(0)


All you have to do is substitution until the recursion terminating conditions are hit:

fib(1) == 1 and fib(0) == 0 so the result is: 5

Cheers,
 
Share this answer
 
Comments
UL UL ALBAB 29-Mar-11 16:28pm    
Cheers...
UL UL ALBAB 29-Mar-11 16:28pm    
Thank you so much...
Sergey Alexandrovich Kryukov 29-Mar-11 16:37pm    
Manfred, I really fail to understand how this can make sense. And how about fib(2)? The OP's code will work correctly after fixing of just two bugs.
Please see my Answer.
--SA
Manfred Rudolf Bihy 29-Mar-11 16:42pm    
Please see my comment on your answer :)
Sergey Alexandrovich Kryukov 29-Mar-11 16:46pm    
Now I see. "Simulate...". Hm...

Anyway, this part of question is scratched, and marked C++ and had 3 bugs in terms of C++, so... I don't think I put it absolutely wrong.

It least, I understand you Answer. Thank you for the explanation.
--SA
Please pay attention: this is C++. "=" means assignment, not the check for being equal. To check is two objects are equal, use "==".

Another bug is this: if n == 1, fib(n - 2) is fib(-1), which you don't want. Hence, you need three branches: for n == 0, 1 more (in the last case, use else; this is the only case when n-2 is valid.

Also, add ';' after each statement.

I thing, if you fix just these two bugs, you code will work.
Don't you consider more general case where first to members are arbitrary "seed value"? Then you need to modify the code.

Note: OP fixed the code in Question after I posted this.

—SA
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 29-Mar-11 16:42pm    
SA, this is not your day I guess. OP was asked in an interview to simulate the execution of that function. That's all there is to it. :)

All I did is show OP how to use substitution to solve that.

Why he tagged it C++ is beyond me though. Maybe it was the job he applied for.
Sergey Alexandrovich Kryukov 29-Mar-11 16:44pm    
Maybe. How the function can be simulated if it has 3 bugs?
Thank you for the note.
--SA
Manfred Rudolf Bihy 29-Mar-11 16:46pm    
It is pseudo code or more probably written down from memory if the interview question really had a C++ function in it.
Sergey Alexandrovich Kryukov 29-Mar-11 16:49pm    
But you did not "simulate" the pseudo-code. You "simulated" correct Fibonacci. So the original code (before OP fixed it), did not what your code did. So my corrections still make sense.
--SA
Manfred Rudolf Bihy 29-Mar-11 16:48pm    
And it is possible to simulate the execution on paper. That the function is syntactically challenged doesn't change a thing here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900