Adobe Interview Question
Software Engineer / DevelopersWell PKT ... you got it wrong.. We always consider probability for limit -> infinity... now here you taking the limited case of 5 function calls.. ur probability just tells about the chance ...it never talks about the outcome..hence here it can give 0 on all these five calls.. but it will maintain the probability for infinity events..
For eg :
lets following be the return of the some first 15 fun6040() of infinite calls..
0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 ........
Here if we take first five calls then for 0 its 100 percent probability but on an avg i.e..15 or infinity it is 60 % i.e.. So your answer is wrong... Saurabh got it right I guess...
I think u have some misconception about Probability...
I have a coin
i flip it
wt is the probability of getting "Head" ?????
I am saying 50%
and u r saying we can't sure abt it until infinite flip.... which is wrong.
for a dice {1,2,3,4,5,6}
wt is the probability of getting 1 ??????
1/6 = 16.66% isn1t it??
ok wt is the probability of getting {1,2,3}
3/6 = 50% ????
still doubt?
I dont have any doubt.. You said everythin right.. But u got it wrong on the number of events..probability of head is 1/2 defintly..but if i toss a coin for first two times..I cant say that i wud get one head and one coin..coz probabilty covers infinite events...and there may be case i wud get two heads on first two tosses becoz its just a probabilty ..we are just guessin the measure of truth in the statemnt..thats it.. I hope you understand the probability..
Anyways If u still not clear..lemme explain u in a more broader way..
Do the followin example practically
Take a coin.. and toss it six times..
Can u be sure that it wud give 3 heads and 3 tails.. I dont think it will give me this combination everytime(inspite both head and tail having 1/2 probability)..it may be 4 heads 2tails...and multiple cases...
So buddy we can not just take probability on these 6 tosses..rather practically we take it for infinite cases..
I hope now u get it..
Maybe we can store an boolean array containing 6 elements, where there are 5 ones and 1 zeros. Every time we run the given function, if it gives zero, then output zero; if it is one, one element from the stored array is output. In this way, the chance of outputting one is 60%*(5/6)=50%.
bool UNIFORM() {
static int count = 0;
bool temp[6] = {1,1,1,1,1,0};
bool input = NONUNIFORM();
if ( !input ) {
return 0;
}
else {
return temp[(count++)%6];
}
}
f1 = 0 with 60% probability.
= 1 with 40% probability.
If we call f1() two times and depending on the return values say that :
0,1 = 0 (60% * 40%)
1,0 = 1 (40% * 60%)
(0,0) and (1,1) = continue.
we will have equal probability for 0 and 1 for the return value of the composite function.
int function f2()
{
int ret = -1;
while(ret == -1)
{
int res1 = f1();
int res2 = f1();
if(res1 == 0 && res2 == 1) ret = 0;
else if(res1 == 1 && res2 == 0) ret = 1;
}
return ret
}
Let we have given fun: int fun6040()
- PKT February 28, 2011if i will execute this function 5 times.... 3 times it will give o/p = '0' and 2 times o/p='1'.
take an Array[6]
Array[0]=fun6040();
Array[1]=fun6040();
Array[2]=fun6040();
Array[3]=fun6040();
Array[4]=fun6040();
Array[5]=1;
now apply randomize on Array[6] you will get any random no. which will have probability 50% for '0' as well as '1'.