Microsoft Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
9
of 11 vote

Well what you are asking is to actually find the nth fibonacci number in O(log n) time which is possible by using the dynamic programming approach.
The matrix { {1,1,}, {1,0} } when raised to the power of n will give the (n+1)th fibonacci number at its (0,0) position.
Here is the working code to find the nth fibonacci number in O(log n) time -

void multiply(int F[2][2], int M[2][2])
{
  int x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];
  int y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  int z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];
  int w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
 
  F[0][0] = x;
  F[0][1] = y;
  F[1][0] = z;
  F[1][1] = w;
}
 

void power(int F[2][2], int n)
{
  if( n == 0 || n == 1)
      return;
  int M[2][2] = {{1,1},{1,0}};
 
  power(F, n/2);
  multiply(F, F);
 
  if( n%2 != 0 )
     multiply(F, M);
}

int fib(int n)
{
  int F[2][2] = {{1,1},{1,0}};
  if(n == 0)
    return 0;
  power(F, n-1);
  return F[0][0];
}

int main()
{
  int n = 9;
  printf("%d", fib(9));
  getchar();
  return 0;
}

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

Like the solution. FYI here the matrix M is [2 2; 1 0] and initial condition I is [3;1]

- axecapone July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, the matrix will be [1 1; 1 0] only. Check it for small cases.
If you raise this matrix to the power of n then the result would be [F(n+1) F(n); F(n) F(n-1)]. Since it is easy to just return the (0,0) element of the matrix, I returned F[0][0] which is actually the "(n+1)th" fabonacci number.

- Spock July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The problem is "like" Fibonacci but not "Fibonacci" because f(n)=2f(n-1) + 2f(n-2). [1 1; 1 0] is for f(n)=f(n-1) + f(n-2)

- axecapone July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh ok, ya for finding 2 times the number you can update the matrix.

- Spock July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@axecapone:  while reading this solution, same thing strike me,plz tell me if f(1)=x and f(2)=y then by what value matrices m[][] and f[][] will be initialize. i.e. what will be initial condition, and how you are finding it. this solution for fibonacci is new for me, i like it..

- mk13 July 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't understand the solution....can anyone pleas explain..
applying this code for n=3, i m getting 3 but it should be 8...how?

- Megha July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@axecapone: how r u getting initial condition I [3;1] and whts it's requirement in this problem??

Awaiting for reply...

- Abhijit August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ohhh... its given in question...

- Abhijit August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@axacapone...
frnd we can left shift the result of fib..function to get our desired result..
but still its not solved in O(log n ) complexity
it takes O(n) time

- shreyans August 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How to put initial condition of 3 and 1

- Anonymous May 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 12 vote

There can be O(1) solution.
This is basically a recurrence relation.
I have solved it & got the solution:
f(n)=a1*(1+3^0.5)^n + a2*(1-3^0.5)^n

where a1= 0.3943375673
a2= 0.1056624327

put the value of n & get output.

- Aashish July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Manan, if you are not able to understand the logic, you can fire questions regarding. I don't think that downvoting will make you understand the logic or you are downvoting only for fun rather than understanding the logic?

- Aashish July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you cant explain the logic, There is no point putting your answer here.... downvoted for no explanation

- loveCoding July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok, let me explain:
f(n)=2*(f(n-1)+f(n-2))

The recurrence relation would look like : r^2=2*(r+1)
i.e. r^2-2*r-2
r=1+3^0.5 & 1-3^0.5
say r1 & r2
Now, f(n) = a1(r1)^n + a2(r2)^n----------------------original
There are two unknowns a1 & a2.
We have,
f(1)=a1(r1) + a2(r2) = 1----------------------equation 1
f(2)=a1(r1)^2 + a2(r2)^2= 3-------------------equation 2

Solve the two equations, you will get the values of a1 & a2.
Put the values of a1,a2,r1,r2 in the original equation, you will get the result.

Hope its clear.

- Aashish July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How did you get "r^2=2*(r+1)" Explain

- loveCoding July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution shondik. Upvoted!
@Manan, Read about recurrence relation equation on Wiki. Explaining it here is beyond the scope of this problem

- axecapone July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik
Dont you think doing (1+3^0.5)^n for large n can lead to precision errors and can sway a lot from the actual answer which would be an interger as the equation func(n) = 2*(func(n-1)+func(n-2)) does not contain any floating point value. This can be solved in O(logn) as proposed by Boson and without any floating point operations.

- Anirban July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

At first glance of the problem, I also come up with this solution. But calculating a1*(1+3^0.5)^n will also need at least log(n) time.

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

3<<(n-1)

- anonymous July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

fusharblog.com/solving-linear-recurrence-for-programming-contest/ It may help :D

- Boson July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not sure. This is what I could see towards the end.

"What remains is how to compute TN-1 efficiently. The most popular method is to use exponentiation by squaring method that works in O(log N) time, with this recurrence:

A^{p} = A \text{, if } p = 1,
A^{p} = A .A^{p-1} \text{, if } p \text{ is odd,}
A^{p} = X^{2} \text{, where } X = A^{p/2}\text{, otherwise.}

Multiplying two matrices takes O(K3) time using standard method, so the overall time complexity to solve a linear recurrence is O(K3 log N)."

- Pavan Dittakavi July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Power of DP O(log n)

Matrix findNthPower( Matrix M , power n )
    {
    if( n == 1 ) return M;
    Matrix R = findNthPower ( M , n/2 );
    R = RxR; // matrix multiplication
    if( n%2 == 1 ) R = RxM; // matrix multiplication
    return R;
    }

- Boson July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No DP is required to do this in log time. DP seems like overkill.

- eugene.yarovoi July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well what you are asking is to actually find the nth fibonacci number in O(log n) time which is possible by using the dynamic programming approach. After that you can just multiply it by 2.
The matrix { {1,1,}, {1,0} } when raised to the power of n will give the (n+1)th fibonacci number at its (0,0) position.
Here is the working code to find the nth fibonacci number in O(log n) time -

void multiply(int F[2][2], int M[2][2])
{
  int x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];
  int y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  int z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];
  int w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
 
  F[0][0] = x;
  F[0][1] = y;
  F[1][0] = z;
  F[1][1] = w;
}



void power(int F[2][2], int n)
{
if( n == 0 || n == 1)
return;
int M[2][2] = {{1,1},{1,0}};

power(F, n/2);
multiply(F, F);

if( n%2 != 0 )
multiply(F, M);
}


int fib(int n)
{
int F[2][2] = {{1,1},{1,0}};
if(n == 0)
return 0;
power(F, n-1);
return F[0][0];
}


int main()
{
int n = 9;
printf("%d", fib(9));
getchar();
return 0;
}

- Spock July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

3 << (n-1)

- anonymous July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

anyone with O(logn) solution...see O(n) solution can easily attained..

- Vineet Setia July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

At first glance of the problem, I also come up with this solution. But calculating a1*(1+3^0.5)^n will also need at least log(n) time.

- songLu July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Are we sure that this is for log(n) complexty.

- Pavan Dittakavi July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

This comment has been deleted.

- Administrator July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is this correct?

- Animesh Sinha July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No

- loveCoding July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Reason?

- Animesh Sinha July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it will be (nlogn)

- loveCoding July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah... figured it out..thanks for pointing out

- Animesh Sinha July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ali Baba, that works. Just an FYI, this can be done in O(n) using DP.

- Pavan Dittakavi July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

By using DP this can be done in log n. Please see my submitted solution.

- Spock July 06, 2012 | Flag


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