## Chegg.com Amazon Interview Question for Consultants Software Engineer / Developers

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

int fib(int N)
{ return (N < 2)? 1 : fib(N-1) + fib(N-2);}

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

``````int fib(int n)
{
int sum;
if(n>0)
sum = n + fib(n-1);
return sum;``````

}

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

sum should be initialized to 0

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

Seems like this returns the sum of first n numbers. Not even related to fib.

btw, what does a 2 line program mean? Idiotic question.

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

@LOLer very true!

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

the question has asked about the nth fibonacci number. the code is written for the sum of n fibonacci numbers.

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

recursive algorithm,
int get_fib(int n)
{
if (n==1)||(n==2)
return 1;
else
return get_fib(n-1) + get_fib(n-2);
end;
}

iterative algorithm,
int get_fib(int n)
{
if (n==1)||(n==2)
return 1;
else
{
prev = 1;
curr = 1;
cont = 2;
while (cont != n)
{
next = prev + curr;
prev = curr;
curr = next;
cont ++;
}
return next;
}
}

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

stupid question. its a simple Fibonacci Qs and the answer is the Standard answer...I bet even the interviewer can't do it in _2 LINES_ LOL
@Gecko : recursive is good enough, no need to go for iterative...
here is the python code:

``````def fib(n):
prev=0
curr=1
for i in range(n):
curr, prev = curr+prev, curr
return curr``````

we start from 0
fib(0) = 1
fib(1) = 1
fib(2) = 2
fib(3) = 3
fib(4) = 5
etc ....

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

long factorial(int n)
{
if (n<1)
return 1;
else
return n * factorial(n - 1);
}

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

Why factorial?

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

both fibonacci and factorial start with a 'F'..i guess thats y ...or may be when u learn recursion in high school u usually make program for factorial and fibonacci on the same day...

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

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

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

http://en.wikipedia.org/wiki/Golden_ratio#Relationship_to_Fibonacci_sequence

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

Better option is to use Matrix method, avoids floating point and the related precision issues.

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

can explain you explain, what you meant by Matrix method?

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

http://www.ics.uci.edu/~eppstein/161/960109.html

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

Also http://www.careercup.com/question?id=57486

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

lol. I think its not asking about squeezing the same code into two line.

http://www.ics.uci.edu/~eppstein/161/960109.html

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

The Matrix method of computing this is interesting.

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

matrix way:

``````[ 1 1 ] n  =   [ F(n+1) F(n)   ]
[ 1 0 ]    =   [ F(n)   F(n-1) ]``````

Plz do not put duplicate questions.

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

To find the nthFibonacci number, use the following recursive definition :
F0 = 0
F1 = 1
When n is an even number :
Fn = Fn / 2(Fn / 2 + 2 F(n－2) / 2)
When n, modulo 4, is 1 :
Fn = (2 F(n－1) / 2 + F(n－3) / 2)(2 F(n－1) / 2 － F(n－3) / 2) + 2
When n, modulo 4, is 3 :
Fn = (2 F(n－1) / 2 + F(n－3) / 2)(2 F(n－1) / 2 － F(n－3) / 2) － 2

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

fib(int n1,int n2,int n,int sum){
if(n-1 > 1){
fib(n2,n2+n1,n-1,n2+n1);
}
else{
System.out.println(sum);
}
}

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

wait to promote your site you jackass

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

@Anonymous on October 08, 2009:
Guys like you promote our sites, we don't have to bother for that job.
You moron, fucking crap

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

@anonymous
int fib(int n)
{
return (n<3)?1:(fib(n-1) + fib(n-2));
}

it should be n<3 you sucker[:P]

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

Mastram, I was going to write it this way.

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

Use iterative version or dynamic programming (e.g. memorization). In recursion you are calculating more e.g fib(4) = fib(3) + fib(2) = fib(2) + fib(1) + fib(1) + fib(0) = fib(1) + fib(0) + .........

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

for(int first=0,second=1,i=1,temp=0;i<=n;i++,temp=first,first=second,second+=temp)
if(i==n && printf("%d\n",second));

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

Just put all code in two lines. Blame the interviewer for inaccurate wording.

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.

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