Yahoo Interview Question
179 Interview Reviews |
Back to all Yahoo Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Intern at Yahoo:
Write a function given x, the function returns the xth number in the Fibonacci sequence.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
1 of 2 people found this helpful
int getFib(int n) {
return n <= 1 ? n : getFib(n - 1) + getFib(n - 2);
}
The interviewer will also want to know you understand that recursion would be slow for high values of n.
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
using namespace std;
int getFib(int n) {
if(n==1||n==2) return n;
return getFib(n-1)+getFib(n-2);
}
int main(){
int n=6;
cout<<getFib(5)<<endl;
system("pause");
return 0;
}
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
1 of 1 people found this helpful
by HappyCoder:
// assume n is positive
if (n <= 2) return n;
int i = 1;
int j = 2;
int k;
count = 2;
while (count < n) {
k = i + j:
count ++;
i = j;
j = k;
}
return k;