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, February 20, 2014

Implement Square Root (Part I)

Problem: Write a function which returns the square root of an integer. Return an integer which is the floor of the integer closest to the value.

int sqrtFlr(int x) {...

Solution:

Let's start off by doing an example:

Let's enter 75. The square root of 75 is between 8 and 9. Therefore, we would return the floor, or 8.

Now, let's think of how we determined the answer was 8. Effectively, we knew squares until we found one that was just below (64) and just above (81) of 75. Then, we returned the lesser. We could imagine turning this into a relatively straight forward implementation of the problem, where we would try every number, until we found one that was just greater, and the return one below.

If we thing of this as an algorithm:

- Start at 1

- Iterate through numbers, squaring each

- Stop when you are greater than your number

- Return one less than the number you squared.

Now, if we code this, we get:

int sqrtFlr(int x) {

int i = 0;

while((i * i) < x) {

i++;

}

return i-1;

}

Now, let's try an example, 80. We then start with i at 0, and end the while loop when i = 9. And then, we correctly return 8.

Now, let's try some other cases. Let's consider when x is itself a perfect square, say 36. In this case we would stop the while lop when i = 6, as 36 is not less than 36. So, this is a bug. We would need to make the correct state i <= 6 to catch this case.

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.