## Amazon Interview Question for SDE1s

• 0

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
1
of 1 vote

This problem could be regarded as fair coin and biased coin problem, please refer to: wiki- fair coin problem

Comment hidden because of low score. Click to expand.
2

``````// 11 -> 0.36, 00 -> 0.16, 10 -> 0.24, 01 -> 0.24
// and discard the case of 11 and 00.
int f1() {
while (1) {
int a = f();
int b = f();
if (a != b) return a;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 2 vote

``````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;
}``````

Comment hidden because of low score. Click to expand.
0

C1 should be updated twice (Probability of 1 = 0.6) and C0 should be updated thrice(Probability of 0 = 0.4).

You have done the opposite in your code.

Otherwise the code and logic are correct

Comment hidden because of low score. Click to expand.
0
of 0 vote

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int f1()
{
static int k = 0;
return k++ % 2 ? f() : 1 - f();
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem could be regarded as fair coin and biased coin problem, please refer to: wiki- fair coin problem

Comment hidden because of low score. Click to expand.
0
of 0 vote

fdsdfssdf

Comment hidden because of low score. Click to expand.
0

Are you left-handed?

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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``````

Comment hidden because of low score. Click to expand.
2

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%

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0
of 0 vote

f1(x) = f(x) + (-1)^x * 0.1

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````void f1(){
int first = -1, second = -1;
//toss twice
//00-4/25,11-4/25, 01-6/25,10-9/25
//check for 00 and 11
do{
first = f();
second = f();
}while(first!=second) //only return 01 or 10
return first;
}``````

Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.