Apples and Oranges?

Question: You have 3 baskets, one with apples, one with oranges and one with both apples and oranges mixed. Each basket is closed and is labeled with ‘Apples’, ‘Oranges’ and ‘Apples and Oranges’. However, each of these labels is always placed incorrectly. How would you pick only one fruit from a basket to place the labels correctly on all the baskets?

Answer: These kinds of questions make for a good warm up in an interview. Frankly speaking, this puzzle just needs a little thought. It really doesn’t take any algorithmic genius.

Say you pick a fruit from the basket labeled ‘Apples and Oranges’. If this fruit is an apple, then we know that since the label is incorrect, this basket only contains apples. Now that we’ve determined that the basket marked as ‘Apples and Oranges’ only contains apples, we can figure out what the other baskets contain. If we look at the basket labeled as ‘Oranges’, we know that since the label is incorrect, this basket either has only apples in it or has both apples and oranges. Since we’ve already established that the basket labeled ‘Apples and Oranges’ contains only apples, we know that the basket labeled as ‘Oranges’ contains both apples and oranges. Then by simple process of elimination, we know that the basket labeled as ‘Apples’ contains only oranges.

You can apply the same logic if you assume you picked an orange from the basket labeled as ‘Apples and Oranges’.

If you have any questions, please feel free to send me an email at [email protected]. If you have any interview questions which you feel would benefit others, I would love to hear about it.

If you're looking for some serious preparation for your interviews, I'd recommend this book written by a lead Google interviewer. It has 189 programming questions and solutions:

Book Cover

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>