Facebook Interview Question for Software Engineer / Developers






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

Why dont you people try recursion as dynamic programming and make it linear :D

- Anonymous September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

space is exponential too, I think. the recursion uses exponential stacks.

- Anonymous April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no...at any given time recursion works on a branch of the tree. and max length of any branch is O(n).

- April 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why dont you people try recursion as dynamic programming and make it linear :D

- wolf September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linear time and constant for storage.

fibonacci(n)
{
int x2 = 0;
int x1 = 1;
int x;

if (n == 0) return 0;

for(int i = 2; i <n; i++)
{
x = x2 + x1;
x2 = x1;
x1 = x;
}

return x2+x1;
}

- Jithu May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or may be I got it wrong, as the question says about recursion.

- Jithu May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

wat nonsense methods u people hav put up, seriously..!!

- harish September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In case of plain-old recursion time complexity is O(2^n) and space complexity is O(n). In case of DP both time and space are O(n).

Note that these complexities are expressed in the value of n and not in the length (number of bits) of n. So O(2^n) is exponential and all the other O(n) complexities are pseudo-polynomial.

In short, it doesn't matter how you implement it, both time and space complexities are exponential. ;)

- Safi December 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Time complexity is O(n)
space complexity: total space used by all,recursive calls : O(n)
fibo(1) and fibo(0) is base case
fibo(3)= fibo(2)+fibo(1) -->2 calls
we need space for local variables and function arguments, but also some space for remembering where each call should return to.The argument at each node in the path is ONE(1) or TWO(2) units smaller than the argument at its PARENT(3). The length of any such path can be at most n, so the space needed by the recursive algorithm is again (some constant factor times) n.

- Miss N April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

time complexity is exponential...space is linear

- Anonymous April 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

0, 1, 1, 2, 3, 5, 8, 13, ...

fib(2) = fib(0) + fib (1) // 1 + 1 + 1 = 3 = 2^1 + 1
fib(3) = fib(1) + fib (2) // 1 + 3 + 1 = 5 = 2^2 + 1
fib(4) = fib(2) + fib(3) // 3 + 5 + 1 = 9 = 2^3 + 1

hence fib(n) - recursive calls = 2^(n-1) + 1

ie. O(2^n) - which is exponential complexity. Looking at the space in similar fashion for each recursive call - we get O(n)

- srinivas April 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but when it comes to fib(5)

fib (5) = fib (3) + fib (4) = 5 + 9 + 1 = 15

it isn't 2^4 + 1 = 17 and less than it.

- C May 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@srinivas ...can u explain hw u caluclated

fib(2) = fib(0) + fib (1) // 1 + 1 + 1 = 3 = 2^1 + 1

fib(0)=0 fib(1)=1 then fib(2) is 0+1=1..plz explain how u got the relation ..thanks

- angad May 28, 2011 | 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