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: hkartika — October 6, 2006
    +4 votes
      + -

    factorial (n)
    {
    if n=0 then return 1
    else return n*factorial(n-1)
    }

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

    Loop is faster than recusive, so, here is a loop version:

    int factorial( int n)
    {
    int i;
    int result =1;

    for (i=n; i>0; i–)
    result *= i;

    return result;
    }

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

    Either iterative or recursive the alghoritm must be aware for the case of a negative parameter.
    The above functions are confused by a negative value: the iterative reports 0 without a warning and worsely the recursive alghoritm will enter an infinite loop

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

    well the above function can be reduced to more cryptic module.

    fact(int n)
    {return(n>0?(n*fact(n-1)):1)}

  5. Submitted By: ankit — October 6, 2006
    -2 votes
      + -

    to a more cryptic one
    fact(n){return fact(n-1)*n+!n;}

    yeah but works only for n>=0.

  6. Submitted By: Mohit — October 6, 2006
    -9 votes
      + -

    My function is gonna work for both +ve and
    -ve integers.

    int factorial (int n)
    {
    int i ;
    int result = 1 ;

    if ( n = 0)
    return result;
    if ( n > 0)
    {
    for (i = n ; i > 0 ; i–)
    result = result*i;
    }
    if ( n < 0)
    {
    for (i = n ; i < 0 ; i++)
    result = result*i;
    if (result > 0)
    result = result - 2*result;
    }

    return result ;
    }

  7. Submitted By: vijaysubramani — October 6, 2006
    +9 votes
      + -

    Mohit.. what the hell does factorial for a negative integer mean ???????

  8. Submitted By: Allen — October 26, 2006
    -7 votes
      + -

    Just use a look up table!! That is the fastest way. :-)

  9. Submitted By: ch — November 17, 2006
    -2 votes
      + -

    int fact(int n)
    {
    return(n>0?(n*fact(n)):1);
    }

  10. Submitted By: ch — November 17, 2006
    +1 votes
      + -

    int fact(int n)
    {
    return(n>0?(n*fact(n-1)):1);
    }

  11. Submitted By: ch — November 20, 2006
    -2 votes
      + -

    a single function
    int a=fact(n);

  12. Submitted By: GigiDuru — March 6, 2007
    +1 votes
      + -

    The factorial of a negative integer is not defined. But I think you’ve better say this to the interviewer BEFORE he tries to trick you into thinking it’s defined.

  13. Submitted By: Sara — April 12, 2007
    +1 votes
      + -

    ok… this one is itr. and faster
    int fc(int i){
    int j, i, factorial=1;
    for (j=i, j>1;j–)
    factorial *= j;
    return factorial;
    }

    but if recursive is prefered then:
    int fc(int i){
    if (i > 1)
    return n*fc(n-1);
    else
    return 1;
    }

  14. Submitted By: Nupur — July 2, 2007
    not yet rated
      + -

    Use Dynamic Programming Please…do not call all these recursive functions again and again..

  15. Submitted By: slice4you — August 2, 2007
    not yet rated
      + -

    #include

    int factorial(int i){

    if (i==0)
    return 1;
    else
    return i * factorial(i-1);

    }

    int main(){

    for (int i =0; i

  16. Submitted By: BS — August 5, 2007
    not yet rated
      + -

    One thing need to be taken care in the above programs is that datatype can be taken as “unsigned int” instead of “int”, considering the fact that we don’t calculate the factorial for -ve numbers.

    It will double the range of the result.
    Both recursive and non recursive implementaions are fine.

  17. Submitted By: goug — October 24, 2007
    not yet rated
      + -

    if you want the accurate result you should use array and stack. the result will be very large when you give an big n for example 10000

  18. Submitted By: Henry Korol — November 13, 2007
    +1 votes
      + -

    I think a lookup table is a legitimate solution, in O(1).
    The table will be pretty small, as for a 32 bit integer for instance, you will be able to calculate the n! for at most n = 32, because n! >> 2^n and 2^32 is the maximal integer to be stored with 32 bits. Actually the n! grows much faster - O(n^n), but 2^n bound is enough for our argument.

    So a lookup table would only need to have 32 entries. Method would check whether n

  19. Submitted By: Henry Korol — November 13, 2007
    +1 votes
      + -

    Continuing from the first post:

    So a lookup table would only need to have 32 entries. Method would check whether n is smaller than 32 and return table[n], otherwise throw error or return 0.

    For the 64 bit integer the bound is likewise 64 (although the real one is intuitively much smaller).

    So yeah, this is a practical O(1) solution in my book.

  20. 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.