## CapitalIQ Interview Question for Consultants

Country: India
Interview Type: Phone Interview

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

Fibonacci sequence is as follow 0, 1, 1, 2, 3, 5, 8.....
the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. so the code is easy.

``````int main()
{
int line = 0;
long long Fibonacci;
Fibonacci = 0;
Fibonacci = 1;
for (int i = 2; i != 20; ++i) {
Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2];
}
for (int i = 0; i != 20; ++i) {
++line;
cout << Fibonacci[i] << "\t";
if (line % 4 == 0)
cout << endl;
}
return 0;``````

}

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

In C:

``````int main()
{
int line = 0, i;
long long Fibonacci;
Fibonacci = 0;
Fibonacci = 1;
for (i = 2; i != 20; ++i) {
Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2];
}
for (i = 0; i < 20; ++i) {
++line;
printf("%4d,", Fibonacci[i]);
}
return 0;
}``````

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

The most optimal way here would be to use Dynamic Programming because the subproblems overlap.
F(n) = F(n-1) + F(n-2)
and in next set of iteration:
F(n-1) = F(n-2) + F(n-3).
So, as you can see, there are unnecessary recompuation of same subproblem, which can be avoided by using Dynamic Programming and storing intermediate results in a table. THe normal fibonacci program won't be of much interest since it incurs heavy complexity (exponential factor).

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

Let n be number of terms entered by user. then we simply use the logic:
for(c=0;c<n;c++)
{
if(c<=1)
next=c;
}
else
{
next=first+second;
first=second;
second=next;
}
printf("%d",next);

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

``````if n == 1
print 0
else if n == 2
print 1
else if n > 2
first = 0
second = 1
n = n - 2
print first and second
while (n-- > 0)
third = first + second
print third
first = second
second = third``````

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

Hi guys, I dont know this answer.

Comment hidden because of low score. Click to expand.
-2
of 2 vote

Is this a Joke. No one ask this simple question in the interview.

``````public static int fib(int n) {
if (n < 2) return n;
return fib(n-1)+fib(n-2);
}``````

Comment hidden because of low score. Click to expand.
2
of 2 votes

First of all, this is a very valid interview question because there are various solutions with different complexities.

And for the program you have written, try to invoke fib(-3).

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

this algorithm is for fibonacc, not negafibonacc (which is considered as different).
Secondly, question does not ask for any particular complexity and if you think recursion is expensive here, how hard is it to convert this into iteration?

Add a Comment
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.

Learn More

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

Learn More

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More