Amazon Interview Question
SDE1sCountry: India
Interview Type: In-Person
while (true)
{
int c0 = 0;
int c1 = 0;
for (int i=0; i < 3; ++i)
{
if (f() == 1)
c1++;
}
for (int i=0;i<2;i++)
{
if (f() == 0)
c0++;
}
return (c1 > c0) ? 1 : 0;
}
f1():
if [f() + {1 - f()}] == 2:
return 1
else
return 0
Lets draw the truth table for it
f 1 f sum f1
0 1 0 1 0
0 1 1 2 1
1 1 0 2 1
1 1 1 3 0
Not quite correct. I drop 1 from your table.
f() f() sum f1() %
0 0 0 0 0.16 (0.4 x 0.4)
0 1 1 1 0.24 (0.4 x 0.6)
1 0 1 1 0.24 (0.6 x 0.4)
1 1 2 0 0.36 (0.6 x 0.6)
So the chance of 0 of your f1() is 52% and 1 is 48%
the thinking is correct, but I think you code is wrong
in you function the value of v never changed after assign v=f(), so your f1() is equal to the original f()
You're right here is correct version:
public int f1() {
int v1 = f();
int v2 = f();
return v1 != v2 ? v1 : f1();
}
BTW: Is it only me who cant edit my own responses? It was possible previously.
This problem could be regarded as fair coin and biased coin problem, please refer to: wiki- fair coin problem
- Anonymous February 14, 2014