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!

Friday, February 24, 2012

Let's Make a Deal

Problem: (Thank you to Mr. Schwartz in 9th grade for this). In the classic TV game show, "Let's Make a Deal," you win a prize if you can choose which of three doors the big prize is behind. After choosing the door, the host opens one of the other doors which has a booby prize (like a bag of sand). You are then asked whether you want to switch to the one remaining closed door, or stick with your original choice. Should you switch?

Solution: Let's start with a drawing of the situation. Pic 1 shows the basics where there are three closed doors, two with booby prizes and one with the big prize behind it. We are asked to choose any door first. Let's say we choose door 2.

Then, the host opens another door, let's say door 1, revealing a booby prize (e.g., a bag of sand). We are then asked if we want to switch -- from door 2 to door 3. We know that one of the remaining closed doors has the big prize, and one another booby prize This situation is shown in pic 2.

Well, should we switch?

The obvious answer is that it just doesn't matter. We know that there is a prize behind one of two doors, and we have a 50% shot. So, switch or don't switch; we should be indifferent and we will have a 50% chance of winning regardless of strategy.

This is a fairly straight-forward answer and is tempting. However, before we accept it, let's think about the situation a little more and if we're using all available information.

Certainly, if we were presented with two doors and told to choose 1, we could make an overwhelming case for the strategy not mattering -- similar to the strategy not mattering if you call heads or tails on a fair coin toss.

However, we were given lots more information here with a longer set up, one choice, some information revealed, and then asked to make another choice.

Unless your an information/logic expert, a good strategy is to try some examples to see what happens when we choose different doors (with big prize, without) and what happens when we switch/don't switch to see if it helps us figure out the optimal strategy:

To win: - If we never switch, we have a 1/3 chance of winning; we need to choose the correct door initially - If we always switch, we have a 2/3 chance of winning; we need to choose the incorrect door initially

Hence, we should always switch to improve our odds of winning.

This answer is correct, but there's something that can be unsatisfying about it, because intuitively, staring at two doors -- which is our situation, it still feels like we should be able to choose either and have a 50% chance.

So, why isn't this the case?

Well, we were given significant information by the door that was opened. Since the door had to be either: 1) the other booby prize (if we chose the first), or 2) could be either other door, we are given information because it cannot simply be a random door that was opened. This information can be used to improve our odds by switching.

If you're interested there are lots of simulations run showing the benefits of switching vs. not switching.

This is a good example of the first, intuitive answer of 50% not being correct, which is usually the case in brainteasers. When stumped, try doing examples, and when possible, the exhaustive answer set, as that can help bring insights into the correct solution.

3 comments:

This was the topic of conversation at a recent dinner party. It can be challenging to persuade someone who is convinced that switching doesn't matter.

Turns out the easiest way for people to wrap their mind around it, was to think of how bad their first guess was. Once they realized their first guess was wrong most of the time (66%), it was a obvious conclusion to always switch.

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.