AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: alibee — October 6, 2006
    -3 votes
      + -

    to return fib of n (printing takes only minor adjustments):

    int fib( int n ){
    if n == 0 return 1;
    else return fib(n-1) + fib(n-2);
    }

  2. Submitted By: smnaha — October 6, 2006
    +3 votes
      + -

    Iterative version:

    int fibo (int n)
    {
    int i, current, one_back, two_back;

    if (n <= 1)
    return 1;
    else {
    one_back = 1;
    two_back = 1;
    for ( i = 2; i <= n; i++) {
    current = one_back + two_back;
    one_back = two_back;
    two_back = current;
    } /* end for loop */
    } /* end else block */
    return current;
    }

  3. Submitted By: tmchow — October 6, 2006
    +4 votes
      + -

    I believe the top most solution would be more efficient if we tested n <=2 rather than checking n==0. This would remove unnecessary iterations.

    int fib( int n ){
    if (n<=2) return 1;
    else return fib(n-1) + fib(n-2);
    }

  4. Submitted By: pt1000 — October 6, 2006
    +2 votes
      + -

    ya, but the sequence is defined as being 0 at the first index, not 1:

    0 1 1 2 3 5 8 …

  5. Submitted By: Philip Mathew — October 6, 2006
    +8 votes
      + -

    Recursion is a disaster for Fibonacci sequence in terms of run time.

  6. Submitted By: christothes — October 6, 2006
    not yet rated
      + -

    print nMembers elements of the series:

    void fib(int nMembers)
    {
    int num1 = 0;
    int num2 = 1;
    int cnt = 0;

    for (cnt=0; cnt<=nMembers; cnt++)
    {
    if (cnt <2)
    {
    printf(”%u,”,cnt);
    continue;
    }
    printf(”%d,”,num2 = num1 + num2);
    num1 = num2-num1;
    }

    }

  7. Submitted By: arunkumar — October 6, 2006
    -5 votes
      + -

    All the above codes can be reduced to a simple form.

    int fib(int n){
    return(n==0?1:(fib(n-1)+fib(n-2)))}

  8. Submitted By: kakes — October 6, 2006
    +2 votes
      + -

    There are better ways to find a solution to this problem instead of using Recursion. Recursion is a disaster as someone has already mentioned. Try giving 20 or more elements in your series and your program will take awful lot of time to compute. For every element in series you’re finding the element before using recursion though you theoretically you would have computed it before!!!

  9. Submitted By: friend — October 6, 2006
    not yet rated
      + -

    int fibo(int n)
    {
    if(n==1

  10. Submitted By: RS — October 11, 2006
    not yet rated
      + -

    void fibo(int n){
    int a=-1, b= 1;

    while(n–-){
    printf(”%d\t”,b+=a);
    a = b-a;
    }

    }

  11. Submitted By: Chantelle — October 14, 2006
    not yet rated
      + -

    void fib(int max) {

    int x = 0;
    int y = 1;

    printf(”%d\t”, b = x);

    for(int i = 1; i *less then* max; i++){

    printf(”%d\t”, b = y);
    int temp = y;
    y = x + y;
    x = temp;

    }

    }

  12. Submitted By: Ant — October 17, 2006
    not yet rated
      + -

    > int fib( int n ){
    > if n == 0 return 1;
    > else return fib(n-1) + fib(n-2);
    > }

    This program will NOT work at all!! Just trace it and you will see that fib(1) = 1 + fib(-1), fib(-1) = fib(-2) + fib(-3), …..

    > int fib( int n ){
    > if (n else return fib(n-1) + fib(n-2);
    > }

    This one is also is not correct! Using this algorithm you will get fib(3) = 2!! But fib(3) = 2 + 1 = 3!

  13. Submitted By: vaibhav — October 18, 2006
    +1 votes
      + -

    int fibo(int n)
    {
    if(n==1 || n==2)
    return 1;

    return fibo(n-1)+fibo(n-2);
    }

  14. Submitted By: Blighten — October 5, 2007
    not yet rated
      + -

    Using the closed form:

    int fib(int n){

    double goldRatio = 0.5 + sqrt(1.25);

    double ans1 = pow(goldRatio,n(double))/sqrt(5);
    double ans2 = pow(1-goldRatio,n(double))/sqrt(5);

    return floatToInt(ans1 - ans2);
    }

  15. Submitted By: Yury — February 6, 2008
    +1 votes
      + -

    Better without recursion! If you can, write the simplest :)

    void fibonacci(int initial/*initial value*/, int count)
    {
    int previous = initial - 1;
    for(int i = 0; i

  16. Submitted By: Yury — February 6, 2008
    +1 votes
      + -

    void fibonacci(int initial/*initial value*/, int count)
    {
    int previous = initial - 1;
    for(int i = 0; i

  17. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.