Amazon Interview Question
Software Engineer / DevelopersI think that both results here are a little off. Even here is 2,4,6 while odd is 1,3,5. Here's my answer:
public static int rollDice ()
{
// Are we going to do even or odd?
int eo; // 0 means we're going to be odd, 1 means even
if ( Math.random() > 0.72 )
eo = 0;
else
eo = 1;
// Generate an odd number (1, 3, 5)
int result = ( (int)( Math.random() * 3 ) * 2 + 1 );
// If eo is 1, adding it to the result makes it even (2, 4, 6); otherwise, it remains odd
result = result + eo;
return result;
}
The simulation program would typically involves generating random number between and 0 and 1 and check in which range it falls in. For equal probability it should be in range of ( 1/6, 2/6, 3/6 ... ) to bias it we can shift the range. i.e for 72 percent we can have ( 0-28/300, 28/300 - 100/300, 100/300 - 128/300 ,...). In this way we can have biased no for even no generator.
when u generate a random number n between 0 and 1, for equal probability, u assign the following way
0 - 1/6 = 1; 1/6 - 2/6 = 2; 2/6 - 3/6 = 3; 3/6 - 4/6 = 4; 4/6 - 5/6 = 5; 5/6 - 6/6 = 6
now for the even numbers to have 72% prob, we have to divide each 1/3rd into 1/3 * 28/100 and 1/3 * 72/100 i.e 28/300 and 72/300
now the ranges will be
0 - 28/300 = 1; 28/300 - 2/6 = 2; 2/6 - 128/300 = 3; 128/300 - 4/6 = 4; 4/6 - 228/300 = 5; 228/300 - 6/6 = 6
this way u get the biased probability of 72% for even numbers
All those fuckhead PhD from GOOGLE, AMAZON, MICROSOFT should design a method how to fuck their mothers. Only then they will stop arguing and opposing you whatever you say. These ass holes should be banned from interviewing. Mother fuckers should interview their own mothers and fathers. They should burn their PhDs as they do no research and waste public money. Fucks should rot in hell.
x=rand()*100;
if(x<=72)
x=(rand()*3)*2;
if(x>72)
x=(rand()*3)*2-1;
I think this works fine
Suppose we want to achieve a probability of p%, where p is an integer between 50 and 100.
Here, we can simply try to generate a random number between 0 and 99, and compare it against p.
There is a standard problem Amazon gives that asks that you generate a uniformly random number between 0 and 6 given a generator that produces numbers between 0 and 4. We can use a similar approach here.
Generate a 3-digit base-6 random number by rolling a die three times. That number will be between 0 and 215 inclusive (in base 10). If the number is larger than 199, throw it away and find another one. Otherwise take its modulo 100, and you have a random number between 0 and 99 to compare to p.
This is with a minimum of 3 die rolls. We need at least that many unless p is some very nice integer (say 75; we can do a probability of 3/4 in two throws).
- We can try by throwing the dice two times.
- Rinks September 19, 2008- Now we have 36 events.. How do we divide the events so that the probability of even number is more than that of odd numbers ?
It can be done like this ..
a. When two consequtive even number comes - consider it even.
b. When we get two odd - consider it odd.
c. When we get two even - taken the second even.
d. When we get one even and one odd - take this even ( this is main).
With the above strategy - there are 27 events with even numbers and 9 events with odd numbers..
So this turns out - 3/4 (Probability for even) and
1/4 (Probability for odd)..