Microsoft Interview Question


Country: India




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

The Theorem which used in calculate Fibonacci number - cost O(lgn).
you can search it on google, basically it's [[F(n+1),F(n)],[F(n),F(n-1)]] = [[1,1],[1,0]]^n.

- siren0413 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How to take care of factor 2.

- MI January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Little knowledge can be dangerous. Hopefully you won't be writing critical airplane software.

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

People got wrong all the time, we all learn from mistakes. So be nice, and if you have time, show your "big" knowledge.

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The "big" knowledge was already posted by Cerebruz.

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wow, seems like someone took that the wrong way.
The idea behind this problem is similar as the way we solve Fibonacci in log(n) time costs, which we have to use matrix multiplication. so, even it has factor of 2, the basic algorithm is almost the same. What we need to do is to modify the Fibonacci algorithm a little bit, then we all set.

The Fibonacci algorithm:

| f(n+1)   f(n)    |  =  | 1  1  |^(n)
|   f(n )  f(n-1)  |     | 1  0  |

Then we take a look at our problem:

| f(n+1)  f(n)  | =| 3    1   | *| 2  1  |^(n-1)
| f(n )   f(n-1)|  | 1  0.5   |  | 2  0  |

To solve our problem, I changed the

| 1   1 |  to  | 2  1 |
| 1   0 |      | 2  0 |

because of the factor of 2. then in Fibonacci serious, the f(1) = 1 and f(2) =1 which is slightly different with our problem, in our problem, f(0)=0.5, f(1) = 1, f(2)=3, so I give it a initial matrix
| 3 1 | to let it start, then make the n to n-1, because
| 1 0 |
we've already got our initial matrix.

again, apologize for my little knowledge, I'm not writing code for airplane so no worries.

- siren0413 January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Someone doesn't like trolls :-)

- Anonymous January 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The quality of the original post, and the fact that one can easily write down the correct matrix instead of the fibonacci one, once you understand the approach, does make it sound like little knowledge.

Of course, looking at siren's other posts (not a huge sample though), looks like siren prefers brevity (unless tricked into explaining things better, like above :-)).

- Jon Bentley January 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL. Whole day to read up act all knowledgeable.

- Anonymous January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

Use this :
| f(n-1) | | 2 2 | = | f(n) |
| f(n-2) | | 1 0 | | f(n-1) |

Matrix exponentiation over it will give f(n) in O(log(n))

- Cerberuz January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution is correct, but there is a small problem with the notation of the vector:

You are multiplying a 2x1 matrix with a 2x2 matrix, and that is not possible.

Either swap places of vector and matrix, or define the vector horizontally (i.e 1x2 matrix).

| f(n-1) f(n-2) | x | 2 1 | = | f(n) f(n-1) |
                    | 2 0 |

- cristian.botau January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, it s correct this way only ( mine was just a representation that looks better ;) )

- Cerberuz January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you explain how it is O(log(n))? You have to multiply the initial matrix [1 3] with [2 1 --- 2 0] n time. How this is O(log(n))?

- musfiqur February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

matrix A = [ f(2) f(1) ]
matrix B = [ [ 2 2 ] [ 1 0 ]
matrix C = [ f(n) f(n-1) ]

A*(B^k) = C where k = n-1

Now kth power of a matrix can be computed in log(k) using repeated squaring ( similar to kth power of an integer ).

- Cerberuz February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can some1 please explain me this que !!! and also the answer that is posted for this que !!:(

- Tejasvi Nuthalapati January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Are you sure func(2) is 3? If func(1) = 1 then if func(0) = 0 func(2) becomes 2 and if func(0) = 1 then func(2) becomes 4. How do you arrive at the value 3?

- Anonymous January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

or were you provided with f(0) by the interviewer? to create a basic matrix for the equation, f(0) is must.

- inheritance January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

f(0) = 1, it is obvious from the the given equation.

f(2) = 2*f(1) + f(0)
=> f(0) = f(2) - 2*f(1)
=> f(0) = 3-2 = 1

Follow the given parameters rather then making any assumptions.

- Cerberuz January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The given equation is
f(n) = 2* ( f(n-1) + f(n-2) )
You've messed up with the brackets

- Inheritance January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh sorry,
f(n) = 2*f(n-1) + 2*f(n-2)
Then also we don't need f(0) to solve as base matrix can be multiplied with appropriate [f(n),f(n-1)] to get [f(n+1,f(n)]. So we can use f(n) = 3 and f(n-1) = 1 where n = 2. Use kth power of base to get f(2+k-1) i.e. f(k+1) or we can say f(n+1)

- Cerberuz January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL! Given a recurrence, and two (required) initial values which define the whole sequence uniquely and you ask if the person is sure?

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And some bright person upvotes...

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What are f(0) and f(1)?

- alex January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(f(n+2)) = (2 1) x  f(n+1)
(f(n+1))    (1 0)     f(n)

=> f(n+2) = 2 * f(n+1) + f(n)

- Kamy January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Multipy(int a[4], int b[4], int c[4])
{
	c[0] = a[0] * b[0] + a[1] * b[2];
	c[1] = a[0] * b[1] + a[1] * b[3];
	c[2] = a[2] * b[0] + a[3] * b[2];
	c[3] = a[2] * b[1] + a[3] * b[3];
}
void Exp(int a[4], int b[4], int n)
{
	if (n == 1)
	{
		memcpy(b, a, sizeof(int) * 4);
		return;
	}
	if (n == 0)
	{
		b[0] = b[3] = 1;
		b[1] = b[2] = 0;
		return;
	}
	int half_n = int(n / 2);
	int c[4];
	Exp(a, c, half_n);

	int c2[4];
	Multipy(c, c, c2);

	if (n % 2 == 1)
	{
		Multipy(c2, a, b);
	}
	else{
		for (int i = 0; i < 4; i++)
			b[i] = c2[i];
	}

}
int main(int argc, char ** argv)
{
	if (argc < 2)
		return 0;
	int n  = atoi(argv[1]);
	int a[4] = {2, 2, 1, 0};
	int b[4];
	Exp(a, b , n - 1);
	int fn = b[0] + b1 / 2; // b1 must be even number, cause fn is a integer
	cout << fn << endl;
	return 0;
}

- speedmancs January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

geeksforgeeks.org/  program-for-nth-fibonacci-number  /

- Kevin February 28, 2013 | 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