Programming brain teasers during job interviews is a common thing for software developers. This subject is too big to fit into a single post and there are books out there handling the same issues. I will only touch on two specific questions you might be asked by your interviewer.
I said upfront that these questions are brain teasers mainly for software developers. The list of questions is quite long but I want to highlight just two.
Programming Brain Teasers
Question 1 :
You have two jugs – 1 has a 5 quart capacity and the other has 3 quarts. They both have irregular shapes – that means you cannot tell by simply looking how full the jugs are unless you fully fill them up with water. Now, with unlimited supply of water, how would you obtain 4 quarts of water using the two jugs?
Solution 1 :
I am going to solve this problem in two ways but you could in as many ways as you want. Let us do it!
Step 1: Fill the 3 quart jug with water – make sure it is totally full.
Step 2: Using the water in the 3 quart jug, fill the 5 quart jug. That means at this point, the 3 quart jug is empty and the 5 quart jug has 3 quarts of water in it!
Step 3 : At this moment, the 5 quart jug has 3 quarts of water meaning it needs 2 quarts to fill it up. Now, fill the 3 quart jug again with water. Using the full 3 quarts jug, fill up the 5 quarts jug(remember it only needs 2 quarts to be full). After doing this step, you will be left with 1 quart in the 3 quart jug.
Step 4 : Right now, the 5 quart jug is full! The 3 quart jug has 1 quart of water in it. Now, empty the 5 quart jug. Then pour the 1 quart from the 3 quart jug into the 5 quart. If you followed carefully, you might have realized how close you are to completing this task. The goal is to obtain 4 quarts of water and you already have 1 quart in the 5 quart jug. You only need 3 more quarts.
Step 5 : At this last step, we start with 1 quart in the 5 quart jug. All we need to do is fill up the 3 quart jug and pour its water into the 5 quart jug. It is now a total of 4 quarts! How easy right?
Solution 2 :
Step 1 : Fill the 5 quart jug with water.
Step 2 : Using the water in the 5 quart jug, fill up the 3 quart jug. At this point, only 2 quarts of water remain in the 5 quart jug and the 3 quart jug is full.
Step 3 : Empty the 3 quart jug. After that, pour the remaining 2 quarts from the 5 quart jug into the 3 quart jug. If you pause and think about it, you realize that you only need 1 more quart to fill up the 3 quart jug. At this point, the 5 quart jug is empty.
Step 4 : Fill up the 5 quart jug. That means you have 5 quarts of water in it. Now to our final step.
Step 5 : Finally fill up the 3 quart jug using the full 5 quart jug(at the moment it already has 2 quarts of water meaning it needs just 1 quart to be full). You are done here because if you fill up the 3 quart jug, you will be left with 4 quarts of water in the 5 quart jug. Ta..dah!
You have 8 pool balls. The problem is, one of them is heavier than the rest. You have two attempts to find the heavier ball using a weight balance. Now show me how to do it!
I bet you are thinking … well, I know this. But after thinking about it, you might realize how tricky it is. Here is the best way to solve it without biting someone’s head off(I don’t think anyone should do this).
Step 1 – Pick six (6) balls at random. After picking 6 balls, you leave 2 balls behind for later. You know you could just weigh those 2 balls right now and if they don’t balance, you pick the heavier one and you are done! Wow, I never thought of that… but assuming we didn’t start with the two balls, let us see our next step with the 6 balls.
Step 2 – Divide the 6 balls into 2 (meaning you have 3 balls on each side of the balance). Remember you only have 2 attempts at finding the heavier ball. You can only use the balance twice. Now weigh 3 balls against 3 balls. If they balance, that means the heavier ball is one of the remaining 2 balls. If that is the case, you just weigh the 2 balls and find the heavier one using your second weighing. What if they don’t balance?
Step 3 – Assuming the two sides didn’t balance on the scale, simply pick the heavier side. That means the heavier ball is in that set of 3 balls. Using your final weighing attempt, move to the next step.
Step 4 – Pick 2 balls at random from that group. Weigh one against the other. If they balance, then the remaining 3rd ball is the heavier ball. If they don’t balance, just pick the one that goes down – heavier one. You are done!
That is all for this post. I hope you found these programming brain teasers during job interviews fun and challenging. If you are preparing for a job interview, I want to say good luck. You can click here to read more about what to do while job searching. Thanks again for stopping by.