# Logic Problems

## Logic and problem-solving scenarios that aren't related to writing code.

# Why Logic Problems?

A logic problem is a general term for a type of puzzle that is solved through deduction. Given a limited set of truths and a question, we step through the different scenarios until an answer is found. While these problems rarely involving coding, they require problem-solving and the ability to articulate plausible outcomes.

You may encounter logic problems during technical interviews for a programming position so it’s worth developing a strategy on how to approach these questions. They’re also a fun way to strengthen your algorithmic reasoning skills!

## Our Question: Apples, Oranges, or Both?

We’ll start with the following problem. You’re faced with three jars labeled “Apples”, “Oranges”, and “Both”. You cannot see the contents of these jars, but you’re informed that *each is mislabeled*. The contents of the jar *are not* decribed by the label.

How many times would you need to draw from a jar in order to accurately label each jar?

### Our Solution: Using the Information

Let’s boil down our problem into factual statements we can use to draw conclusions.

- The jars are mislabeled
- There are three jars
- One jar is a combination of the contents of the other two jars.

We can use these facts to deduce further information which will be instrumental in solving the problem.

First we can rephrase “the jars are mislabeled” as “the jar labeled ‘Apples’ *does not contain Apples*“. It’s the same information but presented in a way that will be easier to work towards a solution.

We should also be drawn to item 3: there’s more information available in the “Both” jar which makes it a more “fruitful” source of inquiry. In general, be aware of any exceptions or abnormalities in the phrasing of the question.

### Our Solution: Filling in Scenarios

Now we’ll begin walking through hypothetical situations. Being able to reason through the problem and articulate your thought process is essential to performing well in a technical interview.

Let’s imagine we draw a fruit from the jar labeled “Apples”. We *know* this jar doesn’t only contain apples, but we’re faced with two possibilities. We could draw an apple *or* we could draw an orange. If we drew an apple, we’d *know* this was the “Both” jar, but what if we drew an orange? Then this jar remains a mystery, either “Both” and we just happened to draw an orange, or it’s purely “Oranges”. We’re still in the dark!

The thought process is the same for drawing from the “Oranges” jar, so now imagine drawing from the “Both” jar. Again, we may draw either type of fruit, but we’ve learned something more substantial. If we draw an orange, we *know* this is “Oranges”. If we draw an apple, we *know* this is “Apples”. There is no ambiguity because the jar is mislabeled as “Both”.

### Our Solution: Drawing Conclusions

We’ve identified one jar, do we need to make additional queries? We should return to our *Use the Information* step. Let’s say we’ve identified “Oranges”.

We have the old “Both” jar, now correctly labeled “Oranges”, and two mislabeled jars: “Oranges” and “Apples”. Can we draw further conclusions? We can!

“Apples” and “Oranges” are both mislabeled, but we have new information. We know where the true “Oranges” is. This doesn’t help us with the mislabeled “Oranges”, it could be either “Both” or “Apples”.

It *does* help with “Apples”. We know “Apples” is not “Oranges” because we’ve already identified “Oranges”. We also know “Apples” isn’t *really* “Apples” because it’s mislabeled. That leaves only one option, this jar is “Both”.

With two correctly labeled jars, the third is easily identified as “Apples”

To wrap it up: “Both” –> “Oranges”, which leads us to “Apples” –> “Both” and “Oranges” –> “Apples”

## Practice Makes Perfect!

We’ll finish this article with a few practice problems for you to try on your own. Each problem has a link which will take you to an explanation of the solution.

### Knights and Knaves

“Knights and knaves” are a popular type of logic puzzle that involves an island inhabited by two types of people: knights and knaves.

- Knights always tell the truth
- Knaves always lie

On the island, you encounter three people, Ted, Ben and Lil.

Ted says, “at least one of the following is true, that Lil is a knave or that I am a knight.”

Ben says, “Ted could claim that I am a knave.”

Lil says, “neither Ted nor Ben are knights.”

Who is a knight and who is a knave?

### Three Fastest Horses

We’d like to find the three fastest horses from a group of 25.

We have no stopwatch and our race track has only 5 lanes. No more than 5 horses can be raced at once.

How many races are necessary to evaluate the 3 fastest horses?