26
$\begingroup$

I had a logical riddle on a programming interview test which was something like this:

In his garage a millionaire had cars, of which only 2 were not white, only 2 were not green and only 2 were not red. How many cars did the millionaire have in his garage?

This seamed quite straightforward to me and I gave an answer, however I wasn't allowed to see my test afterwards and I cannot tell if the answer is correct.

I am curious to see if my answer was correct and also if there is a better mathematical derivation to the problem.

$\endgroup$
  • 1
    $\begingroup$ @AeJey Agree, was my mistake, but still as Florian pointed there are too many answers possible $\endgroup$ – skv Nov 17 '14 at 10:46
  • 3
    $\begingroup$ If you got this question on a programming interview, the answer they were looking for was most likely along the lines of 'the specifications are not specific enough' and they'll want you to ask for clarification. Alot of projects go wrong because programmers interpret requirements in their own way, so a hiring company might be trying to assess the way you go to work. $\endgroup$ – Melchior Philips Nov 17 '14 at 11:48
  • 4
    $\begingroup$ If I used this as a programming task (which I probably wouldn't), then the task would be about requirements-gathering. Unlike puzzling, in programming you must never just decide what an ambiguous phrase means in the way that suits you or that yields the most "sensible" or optimal answer, without at least trying to get a clarification ;-) $\endgroup$ – Steve Jessop Nov 17 '14 at 12:24
  • 2
    $\begingroup$ A more unambiguous formulation would be "all but 2 were white, all but 2 were green, ...". $\endgroup$ – vsz Nov 17 '14 at 16:18
  • 3
    $\begingroup$ All this is assuming that cars can only be one color, correct? $\endgroup$ – RBarryYoung Nov 17 '14 at 18:30
34
$\begingroup$

A total of

three

cars, with colors distributed such that

one is red, one is green, and one is white.

If you take white, then all cars except two (green and red) are white. Likewise, if you take red, all cars except two (white and green) are red. And if you take green, all cars except two (red and white) are green.


Reasoning for this conclusion

If there are more, then the statement that "of them two are not white/green/red" will fail.

Let the total number of cars be $n$
Total number of white cars be $x$
Total number of green cars be $y$
Total number of red cars be $z$

Then $$2 = n-x \rightarrow x = n-2$$ $$2 = n-y \rightarrow y = n-2$$ $$2 = n-z \rightarrow z = n-2$$

So $$n = x+y+z$$ $$n = (n-2) + (n-2) + (n-2) $$ $$n = 3n - 6$$ $$6 = 2n $$

Thus,

n = 3


Note

As far as what I understood from the question "of which only 2 were not white" means there is at least one white car and definitely more than 2 cars. The same for all colors specified in the question. Otherwise the questioner should have mentioned "of which 2 were not white" or "2 cars were not white" or just "none of them are white" instead.

$\endgroup$
  • 16
    $\begingroup$ 3 works. But it is not the only solution. $\endgroup$ – Florian F Nov 17 '14 at 10:36
  • 10
    $\begingroup$ Answer is incomplete. If the millionaire has only two cars, both blue, that also solves the riddle. And that's assuming all cars are monochromatic. $\endgroup$ – Mooing Duck Nov 17 '14 at 21:18
  • 3
    $\begingroup$ The line n = x + y + z is an assumption that is not contained in the question. My first thought - as with @MooingDuck was "two blue cars" - which is a counterexample. So I am afraid your derivation is unsound. $\endgroup$ – Francis Davey Nov 18 '14 at 12:40
  • 1
    $\begingroup$ Yes, I see that. I didn't read it that way though and that assumption is not clearly stated in your derivation. It helps to make clear what assumptions you are making. $\endgroup$ – Francis Davey Nov 18 '14 at 12:57
  • 1
    $\begingroup$ how can one just say n = x + z + y ? I didn't understand that one $\endgroup$ – user5754 Nov 19 '14 at 14:58
68
$\begingroup$

It could be

two cars (both black),

or

three cars, as AeJey explained.

$\endgroup$
  • 5
    $\begingroup$ I believe the wording is compatible with solution 2. The wording is unusual, but so is the whole description of the set of cars. $\endgroup$ – Florian F Nov 17 '14 at 10:46
  • 8
    $\begingroup$ @G.Rassovsky In context I would read "only two where not green" as "Exactly 2 cars is of a different color than green". Two black cars satisfy this, and it's the same for red and white. $\endgroup$ – Taemyr Nov 17 '14 at 11:21
  • 1
    $\begingroup$ It actually depends on the context. In free language, the wording in the question implies that there is at least one white, one green, and one red car. In a mathematical context there is no such implication. $\endgroup$ – Klas Lindbäck Nov 17 '14 at 11:49
  • 3
    $\begingroup$ Almost certainly this is the answer the interviewer wanted. Assuming that each car must be white, red, or green just because those colours are mentioned in the answer is the "ordinary" or "usual" approach. Some programmers include constraints in their solution that nobody asked for. Being able to realize that just two cars will meet the criteria if they are all some non-white nonred nongreen colour is almost certainly the "out of the box" thinking the interviewer wanted. They were probably happier with the logical "3" answer than with "6", or "27", or "mashed potatoes", but happiest with "2". $\endgroup$ – Kate Gregory Nov 17 '14 at 14:37
  • 2
    $\begingroup$ My approach to this sort of problem is to start with edge cases and see if they fit. In this case what if there are no green, red or white cars (=> 2 of some other colour fits)? My reading of it (in English rather than mathematics) didn't seem to me to imply there were any of those colours. $\endgroup$ – Francis Davey Nov 18 '14 at 12:42
22
$\begingroup$

For a total of $n$ cars, of which $w$ are white, $r$ are red, $g$ are green, and $c$ have another colour:

2 or 3.

$$\begin{align} & n, w, r, g, c \in \mathbb{Z}_{\ge 0} \\ n & = w + r + g + c \\ 2 & = n - w \implies w = n - 2 \\ 2 & = n - r \implies r = n - 2 \\ 2 & = n - g \implies g = n - 2 \\ c & \le n \\ n & = (n-2) + (n-2) + (n-2) + c \\ n & = 3n - 6 + c \\ 2n & = 6 - c \\ n & = 3 - \tfrac{c}{2} \end{align}$$ This gives us two solutions at $c=0$ and $c=2$. If $c$ were any bigger, we'd have $c>n$ meaning that our millionaire has more other coloured cars than the total number of cars, which would be quite impossible.

  • If $c=0$ then $n=3 \implies w=r=g=n-2=1$
  • If $c=2$ then $n=2 \implies w=r=g=n-2=0$

So either he has one white, one red, and one green car, or he has two cars that are neither white nor red nor green.

The first corresponds to Aejey's answer, the second one to FlorianF's answer.

$\endgroup$
  • 1
    $\begingroup$ The solutions were given before, but it proves that these are the only solutions. $\endgroup$ – Florian F Nov 19 '14 at 15:52
  • $\begingroup$ @FlorianF well, the OP asked for a better mathematical derivation. $\endgroup$ – SQB Nov 19 '14 at 16:18
  • $\begingroup$ Yes, that was my point. $\endgroup$ – Florian F Nov 19 '14 at 16:33
8
$\begingroup$

Two possible answers hinging on the answer to a question; are red, green and white the only possible colors the cars could be? This meta-question is likely the real test of the puzzle; being able to spot the "hole" in the requirements given by the puzzle (that not all cars in the real world are either white, red or green).

If the puzzle enforces an alternate reality where these three colors are the only options even for millionaires, then the answer is

three cars; one white, one red, one green.

In this case, for each of the three colors, only

one

of the cars can have that color, and therefore two cars cannot.

If we're modelling the real real world, then the answer is

two

cars, all in a color other than white, red, or green, such as black, silver or blue. In this situation, the same cars are not white, not red and not green, because they're black (or blue or silver or yellow or...).

$\endgroup$
  • 2
    $\begingroup$ What about his five dozen cars that each have a white fabric roof, red upholstery, and green body? $\endgroup$ – supercat Nov 17 '14 at 18:41
  • $\begingroup$ This is one of the best answers here, because the prose allows a reader to see the reasoning behind the answer, while still reaching the conclusion on her own. Thus, there is something to learn from this post. Nicely done. $\endgroup$ – Josh Caswell Nov 17 '14 at 19:43
4
$\begingroup$

Given the ambiguities in the question (does it mean just the minimum possible, does it mean that they have to be red, white, green, define red/white/green), I think the only really correct answer is:

Possibly 2 or 3

For what it's worth, I agree with the idea that the test is looking for someone seeing the issues with the spec.

$\endgroup$
  • $\begingroup$ +1, I'd hope we all know what happens when we ass·u·me something. Isn't the response to this "insufficient data"? $\endgroup$ – Mazura Nov 17 '14 at 16:42
  • 1
    $\begingroup$ @Mazura relevant xkcd $\endgroup$ – user3334690 Nov 17 '14 at 18:49
3
$\begingroup$

Let $w$, $g$, $r$ be the numbers of white green and red cars, and $x$ be the number of cars of any other color

$w + g + r + x = total$

$total - w = 2 = g + r + x$
$total - g = 2 = w + r + x$
$total - r = 2 = w + g + x$

$w + g + x = g + r + x$
$w = r$

$g + r + x = w + r + x$
$g = r$

therefore

$w = r = g$

but

$x <= 2 - w - g$
$x <= 2 -2r$
$x <= 2 (1-r)$

$r >= 0$
$x >= 0$
$1-r >= 0$

by the above constraints,
$r <= 1$ AND $r >= 0$

$r = g = w = 1$ and $x = 0$ or $r = g = w = 0$ and $x = 2$

therefore

1 green, one white and one red
or
2 of any other color, and none green white or red

$\endgroup$

protected by Community Nov 19 '14 at 6:09

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.