38

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.

ACB
  • 6,561
  • 3
  • 19
  • 49
G.Rassovsky
  • 876
  • 1
  • 7
  • 15
  • I guess the question is a bit ambiguous as to whether 'only 2 are not' = 'at least one is' (i take it is)... but I was interested to hear your thoughts, and a possible mathematical derivation. Thanks to those who helped. – G.Rassovsky Nov 17 '14 at 10:41
  • 1
    @AeJey Agree, was my mistake, but still as Florian pointed there are too many answers possible – skv Nov 17 '14 at 10:46
  • Updated my answer with the mathematical derivation. – AeJey Nov 17 '14 at 10:59
  • 5
    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 ;-) – Steve Jessop Nov 17 '14 at 12:24
  • 2
    Is it given that the cars are only white, green or red? – Rohinb97 Nov 17 '14 at 14:05
  • Assuming the above, the answer chosen as the correct one is correct, else the answer given is the minimum number of cars he can have. – Rohinb97 Nov 17 '14 at 14:07
  • 2
    A more unambiguous formulation would be "all but 2 were white, all but 2 were green, ...". – vsz Nov 17 '14 at 16:18
  • 4
    All this is assuming that cars can only be one color, correct? – RBarryYoung Nov 17 '14 at 18:30
  • 6
    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. – Melchior Philips Nov 17 '14 at 11:48
  • 1
    The only thing that can be said for certain is that this isn't Chris Evans' garage! – Jon Egerton Nov 18 '14 at 09:12
  • Interesting question! As many have alluded to, I actually think this was more a test of what sort of a thinker you are - the answer could be 2 just as it could be 3, or 4, or infinity. So do you complicate problems, or do you solve them in their simplest form? I think coming up with an answer of 3 requires a far more complex explanation than 2. But that's just me. – Adam McArthur Jul 13 '15 at 12:37
  • @RBarryYoung asking about this in the interview would have been a good move, because it isn't stated anywhere in the puzzle. If this rule is not given, then you can only say that there are at least 2 cars in the garage. – M.Herzkamp Jun 13 '17 at 12:06
  • All my cars except one are Teslas. – Florian F Dec 28 '20 at 22:09

8 Answers8

77

It could be

two cars (both black),

or

three cars, as AeJey explained.

Florian F
  • 29,623
  • 4
  • 63
  • 138
  • the second is not correct too i am afraid, as it cannot be only two cars based on the wording of the question... – G.Rassovsky Nov 17 '14 at 10:37
  • 5
    I believe the wording is compatible with solution 2. The wording is unusual, but so is the whole description of the set of cars. – Florian F Nov 17 '14 at 10:46
  • 9
    @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. – Taemyr Nov 17 '14 at 11:21
  • @Taemyr I got it! I guess that could be a possible answer if the question was worded the way I have it written. However I think the original question had the sense that 'only 2 are not' = 'at least one is'... The problem is that I don't remember the exact wording, I could be misquoting it... But thanks for the clarification, it is also possible that there are a few solutions based on the interpretation. (maybe the interview question is more concerned with which interpretation you choose! ;) ) – G.Rassovsky Nov 17 '14 at 11:36
  • 2
    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. – Klas Lindbäck Nov 17 '14 at 11:49
  • 1
    @KlasLindbäck: I think the puzzle's main clue to context is that the riddle relates to programming. This tells us that almost inevitably the text we see was edited several times, and may have been written by someone who didn't understand the problem at all or (even worse) voted by a committee none of whom understood the problem. Therefore assumptions about sensible free language go out the window ;-) But yes, normally in English saying "2 of them" or "of which only 2" directly implies n > 2, otherwise you'd say "both of them". – Steve Jessop Nov 17 '14 at 12:28
  • 4
    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". – Kate Gregory Nov 17 '14 at 14:37
  • @KateGregory "Almost certainly"? That's pretty presumptuous, considering how simple and un-thought provoking the 2 car answer is. – Red Alert Nov 17 '14 at 22:48
  • if you think most programmers go for the simple solution to problems then you have supervised, mentored, taught, and interviewed a very different crop of them than I have, @RedAlert. – Kate Gregory Nov 18 '14 at 00:38
  • 2
    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. – Francis Davey Nov 18 '14 at 12:42
  • 1
    Not to mention, as a programming-related question, they want to make sure you're capable of considering and accounting for unexpected input. The question (design spec) leads you to consider red, green, and white cars, but the input (the cars in the garage) could be anything, and you have to be able to account for that. – Doktor J Nov 18 '14 at 22:48
  • is it possible to say that he owns 1000 cars for example ? –  Nov 19 '14 at 15:05
  • No. Only 2 are not white, so at most 2 are green. Then, only 2 are not green. This already puts an upper limit of 4 cars. – Florian F Nov 19 '14 at 15:28
  • 1
    He could also have 2 red-white multicolour cars and 2 green cars for a total of 4 cars. He can have an infinite number (or some upper bound based on the minimum value of a car and the fact he is a millionaire rather than a billionaire) of red-white-green multicolour cars independent of any other totals of cars. – MT0 Nov 19 '14 at 15:34
40

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.

AeJey
  • 14,506
  • 5
  • 58
  • 117
  • This seems correct – Legotruck Nov 17 '14 at 10:29
  • Yes, that was my answer. However I am also looking for a mathematical derivation to the problem. – G.Rassovsky Nov 17 '14 at 10:35
  • 22
    3 works. But it is not the only solution. – Florian F Nov 17 '14 at 10:36
  • Updated the answer with mathematical derivation. – AeJey Nov 17 '14 at 11:01
  • 17
    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. – Mooing Duck Nov 17 '14 at 21:18
  • Applicants answering in this manner for a job with Mr Edison, will be "politely thanked for their time and sent on their way." -gabesfascinatingstories IMO, the correct answer is "I don't know, lets go count them." – Mazura Nov 17 '14 at 22:24
  • 5
    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. – Francis Davey Nov 18 '14 at 12:40
  • 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 just "2 cars were not white" instead. – AeJey Nov 18 '14 at 12:54
  • 2
    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. – Francis Davey Nov 18 '14 at 12:57
  • Added that to the answer. Thank you for pointing out the mistake. – AeJey Nov 18 '14 at 13:01
  • 2
    how can one just say n = x + z + y ? I didn't understand that one –  Nov 19 '14 at 14:58
32

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.

SQB
  • 5,118
  • 31
  • 58
9

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

jscs
  • 1,041
  • 10
  • 21
KeithS
  • 474
  • 2
  • 10
  • 3
    What about his five dozen cars that each have a white fabric roof, red upholstery, and green body? – supercat Nov 17 '14 at 18:41
  • 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. – jscs Nov 17 '14 at 19:43
5

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.

Jon Egerton
  • 151
  • 4
4

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

TheRubberDuck
  • 1,193
  • 8
  • 18
1

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.

Gareth McCaughan
  • 119,120
  • 7
  • 313
  • 454
  • 1
    (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.) – Gareth McCaughan Jul 09 '19 at 02:16
0

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.

Admiral Jota
  • 695
  • 3
  • 14