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!

Saturday, February 11, 2012

Boat in a cup

Solution: If you are in a boat, inside of a giant cup which is half-filled with water -- and you throw something from inside the boat into the water, does the water in the cup go up or down?

Let's start with a picture of what this looks like to make sure that understand the problem. Pic 1 shows the problem. There is a boat, afloat on water inside a giant cup.

Now, let's jettison something off of the boat. What happens?

The first thought is that his has to do with buoyancy and how objects float. It's ok to ask the interviewer about buoyancy (I did in fact in an interviews). The most relevant answer here is Archimedes' principle which is:

Any floating object displaces its own weight of fluid.

Well, let's think about this. If we throw something off of the boat, it will displace water equal to its own weight. However, we also know the boat will now also do something different as it has less weight on it.

If you're really good at thinking abstractly, you can actually work Archimedes' principle to the correct answer, but I'm just stuck in theoreticals. When this happens, it's best to go to an example.

Let's assume that we throw something really really heavy and dense off of the boat. (this is a hint too)

Imagine that we throw an anchor off the boat.

Well, intuitively, the boat will come up as it is now carrying less weight of cargo (think of an empty oil tanker rides higher). This is consistent with Archimedes' principle as the boat no longer needs to displace as much water as it weights less.

This one phenomena would result in the water going down in the glass (think of putting your hand in a glass of water and pulling it out).

However, the anchor will displace water which will make it rise (think of putting a big rock in a bucket and seeing it rise).

So, the outcome will depend on whether the water going down will net out more or less against the water going up.

Well, what is it?

Archimedes' will get you here, but often, it's easier to think of a more extreme example if the answer isn't yet clear. Assume that you threw something super heavy off the boat -- like a pea sized amount of a super-dense star.

The result would be that the boat comes up as it is lighter, and the water drops. In the meantime, the pea-size star displaces almost no water, so the net is the water goes down.

This is one possibility. Now, think through if this works for the anchor. In the case of the anchor, the boat goes up, which the anchor displaces water. However, if the anchor displaced as much water as the boat did, it would not sink. So, you can start to see the general pattern that if any object sinks, then the water goes down in the cup.

Now, let's think of if the object floats. Well, this is the special case where the boat goes up causing the water to go down, while the object displaces just enough water to float, which is the same as the amount that it used to displace when it was part of the floating boat. So, there is no change in the water level.

So, the answer is:- if the object floats, the water level is the same- if the object sinks, the water level goes down

This is a good example where the most useful insight came out of an extreme example. Then, we could work backwards and apply principles to quickly solve the problem. One take away is to try to solve a brain-teaser for any case, which can then provide lots of insights.

/* Please let me know if you have other thoughts or find bugs in the solution. Thx, Noah */

2 comments:

Interesting question. However Archimedes' principle is known to not account for surface tension. So I believe your solution is incomplete. To be fully correct a solution should ad least mention surface tension.

After some further thought (and I'm still annoyed I didn't get this), doesn't the answer assume that the volume of the object thrown off the boat is smaller than the volume of water displaced by the boat weighing more? So perhaps it should be: if the object sinks but is smaller than the water displaced by the heavier boat, water level goes down; if the object sinks but is the same size as the water displaced by the heavier boat, or the object floats, water level stays the same; if the object sinks but is larger than the water displaced by the heavier boat, water level goes up.

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.