“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
You have a bucket of jelly beans. Some are red, some are blue, and some green. With your eyes closed, pick out 2 of a like color. How many do you have to grab to be sure you have 2 of the same?
If you pick out three, the worst case is that they are all different colors. So you would have to pick out one more to be certain that you aren’t effected by that worst-case scenario.
This is just the pigeon hole principal. Most questions to the effect of “How many do I need to gaurantee…” can be answered with it.
If you dont know the PHP, I’ll give a shot at teaching it….
Think of the three types of jelly beans as pigeon holes (or cubbie or boxes or whatever.) You have one hole/box/cubbie for each type of jelly bean. Now, start filling them up. Put the first 1 in the first box, put the second in the second box and put the third in the third box, now, when you put the 4th in a box, there is gauranteed to already be one in there. You are gauranteed to have a colision.
You have to grab 4 jelly beans. In worse case first 3 beans will be all of different colors i.e. 1 red, 1 blue and 1 green. And the forth one will be anyone of those colors and hence 2 beans of same colors.
hmm. i dont agree with 4…
We have to assume some number for the three types, say if it has equal number of balls of each color, say 10 red, 10 blue and 10 green. Then you need to pick at least 11 to gaurantee that you have 2 of same color.
If we assume uneual number of balls, say 10 red, 11 blue and 12 green, Then we need (MAX + 1) - so at least 13 balls to pick to be 100% sure that you have 2 of same color.
imti —
when you have picked 2 beans out of the 10 of 1 type arent they of the same color?
the quesn doesnt talk abt picking 2 beans of any specific color - just 2 beans of the same color are needed
Again everyone is over thinking - read the question.
It says.. “How many do you have to grab to be sure you have 2 of the same?”
Does not say “Same COLOR”.
Answer - 2
There’s insufficient information to solve the problem. It doesn’t say how many beans there are overall, how many of each color there are, how the beans are distributed through the jar, or even whether there are equal amounts of each color. Without that information, no specific number can be given, nor can a specific algorithm be formulated.
If you select 4 Jelly beans you are guarenteed that you will have 2 that are the same color.
uhm.. sorry but could you explain this please?
thank you
It is quite simple. If you only take 4 jelly beans, the possible combos that you can come up with are, in terms or a,b and c:
a,b,c,a a,b,c,b a,b,c,c
a,a,b,c a,a,b,b a,a,c,c
b,b,a,c b,b,a,a b,b,c,c
c,c,a,c c,c,a,a c,c,b,b
a,a,a,b a,a,a,c a,a,a,a
b,b,b,a b,b,b,c b,b,b,b
c,c,c,a c,c,c,b c,c,c,c
If you pick out three, the worst case is that they are all different colors. So you would have to pick out one more to be certain that you aren’t effected by that worst-case scenario.
4. Just pigeon-hole principle.. hehe
This is just the pigeon hole principal. Most questions to the effect of “How many do I need to gaurantee…” can be answered with it.
If you dont know the PHP, I’ll give a shot at teaching it….
Think of the three types of jelly beans as pigeon holes (or cubbie or boxes or whatever.) You have one hole/box/cubbie for each type of jelly bean. Now, start filling them up. Put the first 1 in the first box, put the second in the second box and put the third in the third box, now, when you put the 4th in a box, there is gauranteed to already be one in there. You are gauranteed to have a colision.
If you select 2, even if they are of different colors, you will surely get one of these colors in third pick.
pick 4 , this is pigeon hole principle in the other direction
A Simple one.
You have to grab 4 jelly beans. In worse case first 3 beans will be all of different colors i.e. 1 red, 1 blue and 1 green. And the forth one will be anyone of those colors and hence 2 beans of same colors.
You guys are over thinking it. Question states you “picked two of a like color.” Hence, you only need to pick two.
The better question is, how many would you have to pick to guarantee two _different_ colors.
Say,
t = count of type of balls
n = number of items to pick
then solutiomn will be
(t * (n-1)) +1
this will stand if t >= 2
hmm. i dont agree with 4…
We have to assume some number for the three types, say if it has equal number of balls of each color, say 10 red, 10 blue and 10 green. Then you need to pick at least 11 to gaurantee that you have 2 of same color.
If we assume uneual number of balls, say 10 red, 11 blue and 12 green, Then we need (MAX + 1) - so at least 13 balls to pick to be 100% sure that you have 2 of same color.
imti —
when you have picked 2 beans out of the 10 of 1 type arent they of the same color?
the quesn doesnt talk abt picking 2 beans of any specific color - just 2 beans of the same color are needed
ohh sorry ,, i was wrong, ans is (no of colors +1),
in general (no of types +1)
It depends… you might have to pick 50 if you plan on eating 46 before checking the collor.
u pick up the 4 jelly beans.so that 2 beans will be same color.
i think we need to the number of beans of each colour to the total no of beans in the bucket to discuss about this…………………
Again everyone is over thinking - read the question.
It says.. “How many do you have to grab to be sure you have 2 of the same?”
Does not say “Same COLOR”.
Answer - 2
Total number of jelly beans in the bucket - 2.
The answer is at minimum 4 because you may have a hundred beans of each color so you may end up grabbing the same colored beans over and over again.
There’s insufficient information to solve the problem. It doesn’t say how many beans there are overall, how many of each color there are, how the beans are distributed through the jar, or even whether there are equal amounts of each color. Without that information, no specific number can be given, nor can a specific algorithm be formulated.
why not look into the jar, see the ones you want to grab. Close your eyes and grab them?
Leave an Answer/Comment