## Yoak: Will A Real Gold Coin Please Stand Up?

You have a sack of coins of three types: brass, silver and gold. You know that the majority of the coins are gold, though they’ve been painted and partially hollowed so that you can’t actually determine the type of a particular coin. Fortunately, you have a machine into which you can insert two coins and the machine will tell you whether the two coins are of the same type or different types.

Your task is to locate a gold coin.

You will compare the coins in “passes” with each coin not being compared more than once in a pass. In a pass, every coin can be a member of a comparison or not, but any particular coin can’t be part of more than one comparison during the pass. Your goal is to minimize the number of passes required to be sure that you locate a single gold coin.

You should be able to describe how many passes (at most) your solution will require rather than the number of passes increasing arbitrarily with the number of coins that turn out to be in the bag.

## Stephen Morris said,

March 30, 2009 at 6:01 pm

I’m going to take a bite at that there gold coin.

The key point is that most of the coins are gold. We can throw away coins with abandon as long as we know that no more than half of them are gold. Most of the remaining coins will still be gold.

The choices for the first pass are entirely arbitrary. We simply group coins into arbitrary pairs.

If a pair is different then we discard it on the basis that we are discarding at least as many non-gold coins as gold coins. We will still have a majority of gold coins.

This leaves us with only matched pairs.

For the second pass we can take one coin from one of these matched pairs and compare it to a coin from another matched pair. If they are different we discard both pairs, we know that we are discarding at least as many non-gold coins as gold coins, so we will still have the majority of the coins being gold.

Now we have groups of four coins which are matched. Each group has two coins which have not been compared in the second pass. Again we take a coin from each group of four and compare it to a coin from another group of four. Again we throw away groups which do not match.

Now we have groups of eight coins which are matched. Each group has two coins which have not been matched in the second pass. Etcetera, etcetera….

We can keep matching groups until we run out of groups of the same size. Since the size of each group is a power of two the largest group will be all gold.

We have only used two passes.

[/spoiler]

Is this the most efficient way to find a gold coin in terms of the number of comparisons?

How many comparisons would be needed to find every gold coin?

## strauss said,

March 31, 2009 at 2:44 pm

That’s a neat solution! I used twice as many passes, but sorted all the coins out into three piles, one of each kind. All the gold coins are then in the largest pile.

## Stephen Morris said,

April 1, 2009 at 12:26 pm

It makes a difference how you interpret ‘the majority of the coins are gold’. I took it to mean that more than half of the coins are gold.

## jyoak said,

April 1, 2009 at 3:31 pm

I did intend that there more than 50% of all coins are gold. My solution requires that as well.

This solution is better than the one that I intended to offer! Mine requires three passes.

The first step I had envisioned is identical to the one above. Arbitrarily line up coins and compare adjacent pairs, discarding any pairs that don’t match. In the second pass you compare one coin from each pair with a coin of each adjacent pair. (Any odd coin on the end is ok with this strategy.) You have now grouped all adjacent like pairs.

As the final step in this approach, you compare a coin from each group with a coin in the group two away. Since you know the adjacent ones are different, identifying whether the groups two away are like or different is enough to collect all remaining groups with like groups.

That’s three passes and more comparisons to boot.

## Stephen Morris said,

April 2, 2009 at 11:52 am

This is a great puzzle jyoak, is it original?

## jyoak said,

April 2, 2009 at 12:08 pm

Stephen, It isn’t original and I don’t know the source. Puzzles of this sort are common as interview questions for computer programmers and the culture tends to appreciate them and continue to pass them around. This is one such I heard around the water cooler of a programming shop. Another such puzzle that’s been on the site before was in DX. http://mathfactor.uark.edu/2008/05/13/dx-dumb-robots/ This one is anecdotally ascribed to originating from a Microsoft interview question, though I don’t have strong evidence of that.

## czarandy said,

April 20, 2009 at 8:41 pm

A variant of this is that you can make arbitrary comparisons and want to minimize their number. This is basically the “majority element” problem.

In the worst case, I believe you need n-1 comparisons (for n elements).

You could also do the standard probabilistic approach:

1. Pick C random coins (with replacement).

2. Find the majority among them.

3. Output that as the answer.

Unfortunately if you have just slightly more than half of one type and just slightly less than half of the other, this doesn’t work so well. But if you know that there is a fixed proportion of the gold coins (e.g., 50.01%) then you only need to make a *constant* number of comparisons.

## Phil Ryan said,

September 18, 2009 at 10:30 am

A slight problem with the 2 pass solution, if you have an odd number of coins then you are not guarenteed to have more than 50% gold coins after 1st pass. Eg if have 5 coins, 3 of them gold, 2 silver- what happens after your 1st pass with 2 matches?

## jyoak said,

September 18, 2009 at 12:11 pm

Phil,

I don’t see the objection. Could you provide specifics? Perhaps lay out the five coins in order assuming you match from left to right. For instance, one combination is:

GGSSG which has the two matches. You’d match the first two and retain. You’d match the second two and retain. And you always retain the odds. So you have a majority of gold.

To finish the algorithm, you’d then match pair one (GG) to pair two (SS) and they don’t match so you’d discard them. At this point, you have one coin and you know it is gold because in removing you removed at least as many non-gold as gold and you started witha gold majority.

I know this doesn’t necessarily ansswer your objection as you may have a different ordering in mind. Show us how.

## gold coins said,

January 1, 2010 at 3:10 am

This puzzle is very confusing if you can touch the coin you can tell by the weight. The gold coins will always be the heaviest. The gold coin should look different to the others so maybe that would be the best way to distinguish them.

## american eagle gold coin said,

January 2, 2010 at 7:40 pm

I would take a bite out of it . otherwise this puzzle is very confusing i must say

## lunaticg said,

January 10, 2010 at 10:17 pm

What is the answer or solutions?

I am not very good at math but this sound interesting.

## Parallel Minded said,

January 17, 2013 at 10:33 am

Stephen’s 2-pass solution, and his 4-pass extension of Jeff’s solution, depend on a loophole in the problem statement. Jeff’s 3-pass solution does not use the loophole.

The problem statement refers to “passes”, for which certain properties are listed (sounding like clarifications rather than an exhaustive list of properties). A very natural unlisted property for a pass would be that the comparisons in a pass are essentially simultaneous, so in particular you cannot use results from the “first part” of a pass to decide what coins to compare “later” in that same pass. This property is natural if you think of a pass as being done in one step on a parallel computer, and of course you want to minimize the number of passes because comparisons take a long time and you want to minimize the time of the parallel algorithm. Indeed, without this property, it seems much less natural to want to try to minimize the number of passes. (Do you have a data structure that decays each time it gets compared?)

(If I were to ask this question in an interview, and the applicant used this loophole, I would ask them for an example of when their interpretation could be relevant!)

In any event, Stephen’s nice and tight analysis prompts the non-loophole version of the same question:

How many parallel passes does it take to separate all the coins by type into three piles?## Stephen Morris said,

January 18, 2013 at 6:48 pm

Parallel Minded: Yes, you make a good point, why would you be concerned about the number of passes if each pair is compared sequentially? Unless the coins were affected by the machine maybe? Seems unlikely. For the record the problem talked about a machine that took two coins at a time but I agree that then we should be looking at the number of comparisons. Now for your question, [spoiler] I think straus already answered it with four passes. Arrange the coins in a circle. Use two passes to compare each coin with it’s immediate neighbours. We now have contiguous groups of like coins, each group known to be different to the neighbouring groups. We use two more passes to compare each group to the groups which are one away. Label one group as A and work clockwise. The next group we label B. Going round the circle we can identify each group as A, B or C.[/spoiler] It’s a fun part of these types of puzzles to try different variations and see what difference they make to the answer. One obvious change is to get rid of passes altogether and just think about how many pairs we should compare. Or we could look at the average number of comparisons, rather than the maximum. In my answer above I have assumed we can take knowledge from one pass and use it to define the next pass, but what if we can’t? What if we must define the comparisons in each pass before we start? (maybe this was your intention) I don’t know the answer to these questions!

## Parallel Minded said,

January 29, 2013 at 1:34 pm

Stephen: Ah, nice! I hadn’t noticed that [spoiler]

the 3rd and 4th rounds work fine even for groups of a single coin, because you can group the comparisons like this:

Round 3:

1 3

2 4

5 7

6 8

…

Round 4:

3 5

4 6

7 9

8 10

…

[/spoiler]

The last question you ask is interesting, it’s like sorting algorithms where all the comparisons need to be specified ahead of time: Sorting Networks.

But I think it has a rather different answer… [spoiler]

You need to compare every possible pair of coins.

If there are any two coins A and B which are not compared with each other, then suppose all other coins (call them “class C coins”) are equal to each other but unequal to A or B. In this case you cannot know whether A and B are the same or not.

[/spoiler]

Would it help if you are told there is at least one of each type of coin?