Expedia Interview Question for Software Engineer / Developers






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

// Recursive approach. Not efficient

public int fibonacci( int num ) {
return (num <= 1) ? num : fibonacci(num - 1) + fibonacci(num - 2);
}

// More efficient iterative approach
public int fibonacci( int num ) {
if( num == 0 ) return 0;
long[] fib = new long[num + 1];
fib[0] = 0;
fib[1] = 1;
for( int i = 2; i <= num; i++ ) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[num];
}

- Jimmy December 10, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Jimmy Jose, your method are not efficient at all.
Equation F(n) = F(n - 1) + F(n - 2) - looks pretty much like recursion, but in this case you shouldn't use recursion. That's the whole point.

int fibonacci(int N) 
{
	int first = 0;
	int second = 1;
	
	if(N <= 1) { return first; }
	
	int i = 1;
	int current = second;
	while(++i < N) {
		current = first + second;
		first = second;
		second = current;
	}
	return current;
}

- zdmytriv March 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To find the nth Fibonacci number, use the following recursive definition :

* F0 = 0
* F1 = 1
* When n is an even number :
Fn = Fn / 2(Fn / 2 + 2 F(n-2) / 2)
* When n, modulo 4, is 1 :
Fn = (2 F(n-1) / 2 + F(n-3) / 2)(2 F(n-1) / 2 - F(n-3) / 2) + 2
* When n, modulo 4, is 3 :
Fn = (2 F(n-1) / 2 + F(n-3) / 2)(2 F(n-1) / 2 - F(n-3) / 2) - 2

Thanks
Ankush

- YetAnotherCoder October 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Public int fib(int n)
{
	If(n<=2)
	{
		Return 1;
	}
	Else
	{
		Return fib(n-1) + fib(n-2);
	}
}

- Anonymous February 16, 2015 | 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