Goldman Sachs Interview Question for Software Engineer / Developers






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

It is the implementation of Fibonacci series 0,1,1,2,3,5,8,13........
F0 =0;
F1 =1;
F2=1;
F3=2;
......
F4=F3+F2;
F4 would 2+1 i.e 3

So u can come up with a C program to implement the fibonacci series which is something like below.

a[1] = 0;
a[2] =1;

for (i=2;i<=n;i++)
{
a[i] = a[i-1]+a[i-2];
printf("%d", a[i]);
}

- Anonymous July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This has appeared countless times before. O(log n) is possible.

- LOLEr July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use Matrix Exponentiation ...... to get it in logn

- Syam July 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please give the O(log n) solution?

- Anonymous July 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This site has a search functionality (top right corner). Use it.

- LOLer July 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nob

- Anonymous July 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the idea of usign matric multiplication comes from the fact that the fibonacci number f(n) has a relation with the nth exponentition of a 2x2 matrix with all 1 baring the right bottom as zero...and as we know matrix multiplication is essentially divide-n-conquer mechanism, the complexity is just O(logn), HTH

- LLOLer August 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use recursion
=================

function_fib(x){

if(x==0) return 0;
if(x==1) {print 1; return 1;}
if(x==2) {print 2; return 2;}

y=function_fib(x-1)+function_fib(x-2);

print y;

return y;

}



T(n)=T(n/2)+T(n/2) +O(1)
T(n)=2T(n/2) +O(1)

Complexity=O(ln n)

- d3vilking September 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Incorrect.

Infact: T(n)=T(n-1)+T(n-2)
and T(n-1) != T(n/2) unless n = 1

So ur conclusion is incorrect

- Yuva October 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its actually exponential with recursion O(2^n)

- Yuva October 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, it is exponential, but it is probably O(r^n) where r is the golden ratio.

Exercise: What if we save the intermediate results and do an O(1) lookup before trying to compute? What is the complexity then?

- LOLer October 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Did u know there was a formula to compute Fib(n)...

Taking r5 = sqrt(5);
f(n) = ( [(1+ r5)/2]^n - [(1- r5)/2]^n )/r5
So basically its an O(1) algorithm !!!

See second ans here for solution
http://stackoverflow.com/questions/360748/computational-complexity-of-fibonacci-sequence

- Yuva October 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Incorrect.

It is not O(1).

Moreover, it uses floating point arithmetic.

- LOLer October 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

T(n)= T(n-1) + T(n-2)
T(n-1)=T(n-2)+T(n-3)
T(n-2)= T(n-3)+ T(n-4)
Replaceing
T(n)=T(n-2)+ 2T(n-3)+T(n-4)
.......take case of k-1
T(n)= T(n-(k-1))+ (k-1) T(n-((k-1)+1)) + T(n- ((k-1)+2))
we can write this way .. very by keeping k =2

take k=n now
T(n)=T(1)+(n-1) T(0)+ T(1)
T(n)= O(1)

- komal May 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@komal :) immediately required a maths tutor.

- Anonymous July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i agree with u

- unknown July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup
how come (n-1)*T(0)=O(1)

- Anonymous July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int fib(int n)
{
  int a = 0, b = 1, c, i;
  if( n == 0)
    return a;
  for (i = 2; i <= n; i++)
  {
     c = a + b;
     a = b;
     b = c;
  }
  return b;
}
 
int main ()
{
  int n = 9;
  printf("%d", fib(n));
  getchar();
  return 0;
}
Time Complexity: O(n)
Extra Space: O(1)

- Nit April 11, 2011 | Flag Reply


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