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, March 16, 2012

Brainteaser: The two paths

Problem: A traveler comes to a fork in the road. One path leads to the land of L, where people always lie, the other path to the land of T, where people always tell the truth. At the fork is a man, but the traveler does not know if the man is from L or T. We can ask him only one question.

What question should the traveler ask to ensure he gets to T?

Solution: Let's draw a picture, as this often helps find the solution.

Pic 1 shows the problem, where we have encountered a man from L or T and we are trying to find the correct path to T.

If the man is from L, he will always lie. If he is from T, he will always tell the truth. We have to determine a question, which will allow us to know which way to go, irrespective of whether he is from L or T.

It's better not to just start randomly guessing (unless you immediately see the solution), so let's try to think structurally about what we are trying to get out of the question:

1. The question must allow us to choose the correct path2. The answer must be definitive3. The answer must not depend on whether the person is from L or T. It must be definitive irrespective of the person's home.

Most obvious questions fail this test, e.g.,:- Which path takes me to T? -- fails because we do no know from the answer where to go- Which path takes me to L? --If we could get this answer, we could do the opposite, but again, fails--Are you from L or T?-- They both say T in this answer, and we don't know where to go?

Let's think about a two question combination that could work. This is in fact trivial.ny question that uniquely determines where the person is from, e.g.,:What is 2+2? to determine if the person is from L or T, thenWhich path goes to T?

Unfortunately, we don't have two questions (and we can't put both in one question). One thought here is to try to ask a question that reveals both the person and answers something about the path, like: What path is the best path? But again, this doesn't work either.

At this point, let's look at the two question combination? Is there anything that we don't need in there? Well, one thing is that we actually figured out too much -- we don't actually need to know what city the man is from.

So, with this insight, we simply need to know the right path, and we have essentially a true and a false. We can use these to cancel each other out.

For example, we can ask the man, "What path would the other person say goes to T?" Hence, each will give the path to F." Then, we can take the opposite.

There is nothing terribly tricky about this problem. The important thing is to be structured, not just start guessing and to try to glean insights of what is needed and not needed. Some people find truth tables helpful in this problem, but I never have.

Also, there are other questions, but they typically involve negating either with someone from 'your town' or 'the other town', e.g., "What path leads to your town (negating with yourself)?"

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.