38
$\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 seemed 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$
13
  • 6
    $\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$ Commented Nov 17, 2014 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$ Commented Nov 17, 2014 at 12:24
  • 2
    $\begingroup$ Is it given that the cars are only white, green or red? $\endgroup$
    – Rohinb97
    Commented Nov 17, 2014 at 14:05
  • 2
    $\begingroup$ A more unambiguous formulation would be "all but 2 were white, all but 2 were green, ...". $\endgroup$
    – vsz
    Commented Nov 17, 2014 at 16:18
  • 4
    $\begingroup$ All this is assuming that cars can only be one color, correct? $\endgroup$ Commented Nov 17, 2014 at 18:30

8 Answers 8

40
$\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$
11
  • 22
    $\begingroup$ 3 works. But it is not the only solution. $\endgroup$
    – Florian F
    Commented Nov 17, 2014 at 10:36
  • 17
    $\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$ Commented Nov 17, 2014 at 21:18
  • 5
    $\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$ Commented Nov 18, 2014 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$ Commented Nov 18, 2014 at 12:57
  • 2
    $\begingroup$ how can one just say n = x + z + y ? I didn't understand that one $\endgroup$
    – user5754
    Commented Nov 19, 2014 at 14:58
77
$\begingroup$

It could be

two cars (both black),

or

three cars, as AeJey explained.

$\endgroup$
14
  • 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
    Commented Nov 17, 2014 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
    Commented Nov 17, 2014 at 11:21
  • 2
    $\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$ Commented Nov 17, 2014 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$ Commented Nov 17, 2014 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$ Commented Nov 18, 2014 at 12:42
32
+100
$\begingroup$

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 && \text{From $\eqref{eq1}, \eqref{eq3}, \eqref{eq5}$}\\ \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.

$\endgroup$
8
  • 1
    $\begingroup$ The solutions were given before, but it proves that these are the only solutions. $\endgroup$
    – Florian F
    Commented Nov 19, 2014 at 15:52
  • $\begingroup$ @FlorianF well, the OP asked for a better mathematical derivation. $\endgroup$
    – SQB
    Commented Nov 19, 2014 at 16:18
  • $\begingroup$ Yes, that was my point. $\endgroup$
    – Florian F
    Commented Nov 19, 2014 at 16:33
  • $\begingroup$ Lovely edit, SQB! Glad to see you are trying to make your answer the best it can be :) $\endgroup$
    – Mr Pie
    Commented Jul 9, 2019 at 10:04
  • 1
    $\begingroup$ @MrPie thanks! It got a couple of upvotes today, which reminded me of its presence. $\endgroup$
    – SQB
    Commented Jul 9, 2019 at 10:07
9
$\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
  • 3
    $\begingroup$ What about his five dozen cars that each have a white fabric roof, red upholstery, and green body? $\endgroup$
    – supercat
    Commented Nov 17, 2014 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
    Commented Nov 17, 2014 at 19:43
5
$\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$
2
  • $\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
    Commented Nov 17, 2014 at 16:42
  • 2
    $\begingroup$ @Mazura relevant xkcd $\endgroup$ Commented Nov 17, 2014 at 18:49
4
$\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$
1
$\begingroup$

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.

Otherwise

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.

$\endgroup$
1
  • 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
    Commented Jul 9, 2019 at 2:16
0
$\begingroup$

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.

$\endgroup$

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