Code WorkOut of the Day -- Daily exercises for programmers looking for jobs, or just to keep sharp with skills and have fun.
I give talks, like this: https://youtu.be/NpvTE7GlXSM for people looking for jobs, or groups of programmers preparing for M&A tech HR due diligence.
Follow us on twitter: @codewod

How to

When solving these problems, learning by doing is much better than learning by reading. I encourage to you read only as far in the solution as you need, then trying to solve the problem. If you get stuck, try reading a little further. And of course, let me know if you find a better solution!

Thursday, May 17, 2012

Next Fibonacci number (non-recursive)

Problem: Write a non-recursive function to prints the xth Fibonacci number which can do it in linear time

int finNum(int num) {...}

Solution: The last two problems go into the Fibonacci sequence, so let's start by thinking about how this could be implemented non recursively.

We would need to iterate and always keep track of the two previous numbers, and then finish up with the new Fibonacci number. And, if we pay attention to error cases, this should be straight forward.

The iteration step needs to be done in the correct order and can be a little tricky, so you may want to write out an example, say 4, and then try it.

Well, that's it. What's the run time? Since we go through the lop up to num-2 times, it is num. We should check boundary cases of num = 2, and an error.

Also, let's test with the number 5 -- and it works.

So, we have now implemented a non-recursive solution. Let's talk about the trade-offs. First, it's longer than the simple fib implementation. But, it is efficient (linear) and does not have the recursive overhead.

The boundary cases can be tricky, and it doesn't read as nicely as the simple recursive Fibonacci solutions.

This is a problem that seems really easy, but it's important to always check the boundary cases and do an example, as trying to figure out all the fib0, fib1 and fib otherswise and where to initialize the variables, is very tricky otherwise, but fairly simple with an example.

No comments:

Post a Comment

30 minutes, once a week, free

To help the community, I do one 30 minute interview every week for free for candidates (virtual, over some online coding tools). Email me if you're interested. It's good practice for the kinds of questions you'll get, and I'll give honest feedback as to your skills, likelihood of getting a job, and tips for improving/practicing for future interviews.

I especially like to work with minority candidates, and those looking to make career changes, so please shoot me an email, provide some background, and we'll set up a time.

noah@codewod.com

Also, if you find alternative or better answers to the questions, please let me know and I'll post/credit!

About Me

About me: I'm currently the founder of Workhood (www.workhood.com) and have spent over 10 years in the technology industry. Prior to Workhood, I was the co-founder of SocialShield, acquired by Avira (A/V security company with over 100m users), where I was subsequently the VP Technology/Operations. I worked previously in McKinsey's technology practice in London and New York and also worked at several start-ups as an engineer and product manager, founding my first venture backed company when I was 22 -- and have raised over $35M in my career in financing. My focus is always on delivering great products quickly and I am a huge proponent of agile programming/product development and the lean start-up techniques. I'm a Stanford CS grad, always interested in coding problems -- co-author of the best-selling book: Programming Interviews Exposed: Secrets to Landing Your Next Job. I also went to HBS and am an avid college football and soccer fan.

I blog about coding problems at: www.codewod.com and about start-ups at www.noahkindler.com. I live in the Bay Area and can often be found at Crossfit or on the slopes when I manage to sneak out.