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.

  • 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
  • 4
    $\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
  • 5
    $\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

A total of


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 $$


n = 3


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.

| improve this answer | |
  • 18
    $\begingroup$ 3 works. But it is not the only solution. $\endgroup$ – Florian F Nov 17 '14 at 10:36
  • 15
    $\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
  • 4
    $\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
  • 2
    $\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
  • 2
    $\begingroup$ how can one just say n = x + z + y ? I didn't understand that one $\endgroup$ – user5754 Nov 19 '14 at 14:58

It could be

two cars (both black),


three cars, as AeJey explained.

| improve this answer | |
  • 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
  • 9
    $\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
  • 4
    $\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

2 or 3.

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

$$\begin{align} n & , w, r, g, c \in \mathbb{Z}_{\ge 0} \tag{1} \label{eq1} \\ n & = w + r + g + c \tag{2} \label{eq2} \\ n & \ge c \tag{3} \label{eq3} \\ 2 & = n - w \implies w = n - 2 \tag{4a} \label{eq4a} \\ 2 & = n - r \implies r = n - 2 \tag{4b} \label{eq4b} \\ 2 & = n - g \implies g = n - 2 \tag{4c} \label{eq4c} \\ n & = (n-2) + (n-2) + (n-2) + c && \text{From $\eqref{eq2}, \eqref{eq4a}, \eqref{eq4b}, \eqref{eq4c}$}\\ n & = 3n - 6 + c \\ 2n & = 6 - c \\ n & = 3 - \tfrac{c}{2} \tag{5} \label{eq5} \\ c & = 0 \land n = 3 \Large \lor \normalsize c = n = 2 \end{align}$$

As we see, when taking $\eqref{eq1}$ and $\eqref{eq3}$ into account, $\eqref{eq5}$ has 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 solution corresponds to AeJey's answer, the second one to FlorianF's answer.

| improve this answer | |
  • 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
  • 1
    $\begingroup$ @MrPie thanks! It got a couple of upvotes today, which reminded me of its presence. $\endgroup$ – SQB Jul 9 '19 at 10:07
  • 1
    $\begingroup$ @MrPie wow, thanks! $\endgroup$ – SQB Jul 18 '19 at 14:25

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


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

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


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...).

| improve this answer | |
  • 3
    $\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$ – jscs Nov 17 '14 at 19:43

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.

| improve this answer | |
  • $\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

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$


$w = r = g$


$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$


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

| improve this answer | |

All the answers already given either (1) don't explain why the answer is what it is or (2) try to do so by setting up equations and doing a pile of algebra. Here's another approach. First of all, I assume every car is of exactly one colour (which may or may not have been a deliberate ambiguity in the original question).

First of all, there are at least two cars (since there are e.g. two that aren't red). If there are exactly two cars then two (hence both) the cars are not red, two (hence both) the cars and not green, and two (hence both) the cars are not white. So if there are exactly two cars then they are of colours other than red, green and white, and clearly in this case the given conditions hold.


there are at least three cars. At most two of them are not red, hence at least one is red; ditto for green and white. So we have at least one car of each of these colours, and these of course provide two that aren't red, two that aren't green, and two that aren't white. So there can't be any more cars (if we do, and e.g. it isn't white, then we have too many cars that aren't white). Hence, if there are more than two cars then there are exactly three, one of each of the specified colours.

Personally I find this clearer than all the algebra, though SQB's calculation does have its own appeal.

| improve this answer | |
  • 1
    $\begingroup$ (My attention was drawn to this question because of Mr Pie's bounty for SQB's answer. My posting this one shouldn't be taken to mean that SQB's answer doesn't deserve that bounty; just that it seems to me there's a bit of a gap in the range of solutions here and I thought it worth filling.) $\endgroup$ – Gareth McCaughan Jul 9 '19 at 2:16

I propose that this millionaire might have

arbitrarily many cars.

Among these cars

all but two of them are white with red and green polka dots,

while the other

two cars are both blue.

| improve this answer | |

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