HCL America Interview Question for Software Engineer / Developers






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

This can be made fast if you use this property of Fibonacci numbers:

A number F is fibonacci if and only if one of 5F^2+4 or 5F^2-4 is is a perfect square.

- LOLer August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or maybe it just faster to do binary search, using O(logn) algorithm for fibonacci.

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

I dont quite get it ? Binary search on what ?

- knap August 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the first response is understood but dint get the binary search technique. Please explain in detail.

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

@kanp & lickie
By binary search he might be referring to check/look for the given number by doing binary search on a Fibonacci list, assuming we've this list at our disposal, right @ LOLer ? but i guess we dont have the list(partial) to make use of this technique.

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

First notice that fibonacci numbers form a increasing sequence.

Now, suppose you are given a number F.

You guess an n and calculate the nth fibonacci number, fib(n).

If F < fib(n), reduce the value of n in the standard binary search way
if F > fib(n), increase the value of n in the standard binary search way.

fib(n) can be calculated in O(log n) time using the matrix method.

So if F was such that fib(k) <= F < fib(k+1), you take O(log k) calls to the calculation of a fibonacci number to determine if F is fibonacci.

Thus O(log k) calls to calculate fibonacci, each being O(logk).
Thus the total time complexity is O(log^2 k).

Also, k = O(log F).

Thus time complexity if O( log(log F) * log (log F))

The normal 5F^2 +/- 4 perfect square detection algorithm probably takes O(log F) time. Smarter algorithms for detecting if 5F^2 +/- 4 is a perfect square probably end up calculating Fibonacci-type* numbers anyway...


(Fibonacci-type = numbers which can be calculated using the matrix method, for lack of a better word)

- LOLer August 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Finding out whether a number is a perfect square does have logF complexity (for the 5F^2 +/- 4 solution). If this operation *is* hardware supported (?) then its definitely a better solution.

- Mohit August 28, 2009 | 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