Big Fish Interview Question for SDE-2s


Country: United States




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

public static int random200() {
int a=f1();
int b=f1();
int c=f1();
int d=f1();
int e=f1();
int f=f1();
int g=f1();
int h=f1();

int r=a+b*2+c*4+d*8+e*16+f*32+g*64+h*128;

if(r<=200 && r>=1) {
return r;
} else {
return random200();
}
}

- baoxin.zhou January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your function might be stuck in recursion as long as the generated number r is above 200, which could be a very long time (or forever if random generator is flawed)

- gen-x-s January 10, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static int f200(){
	int i=1;
	int ret=0;
	while(i++<8){
		ret+=f1();
		ret<<1;
	}

	if(ret>=1 && ret <=200) {
		return ret;
	} else {
		return f200();
	}
}

- baoxin.zhou January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry,

ret<<1

, should be changed to

ret<<=1

- baoxin.zhou January 04, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok, idea is clear.

- zr.roman January 04, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think that your solution is correct. I had a similar solution in mind, you just confirmed it validness. Thanks.

- holdnet January 04, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int f200()
{
	int ret = 0;
	for(int I = 1; I < 200; ++I )
		ret += f1()
	return ret;
}

- anon January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think the return value is between 1 and 200 with equal probability.

- baoxin.zhou January 04, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Ruby:

def f200
result = 0
factor = 0.5
16.times do
result += factor * f1()
factor /= 2
end
return (result * 200).to_i + 1
end

- Kin Lum January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Ruby:

def f200
  result = 0
  factor = 0.5
  16.times do
    result += factor * f1()
    factor /= 2
  end
  return (result * 200).to_i + 1
end

- Kin Lum January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def f200
  result = 0
  factor = 0.5
  16.times do
    result += factor * f1()
    factor /= 2
  end
  return (result * 200).to_i + 1
end

- Kin Lum January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Ruby:

def f200
  result = 0
  factor = 0.5
  16.times do
    result += factor * f1()
    factor /= 2
  end
  return (result * 200).to_i + 1
end

- kenneth.kin.lum January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about: f200 = 199 * f1 + 1 ?

- sorin January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

f200 = 199 * f1 + 1

- sorin January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flip the coin every time to see if the number is in the lower half of the range, or upper half. Then recurse.
E.g. 200 =>
< 100? =>
50<= x < 100 =>
etc.

- Anon123 January 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import random as rd 
    
    def prob200(half):
    	if half >= 200:
    		return 0	
    	#f1 can be replaced by this rd.randint(0, 1)
    	toss_value = f1() 
    	# check if toss falls in this current half and then change the half for next recursion. 
    	# we change half from 1, 2, 4, 8, 16, 36, 64, 128
    	result = toss_value*half + prob200(half<<1)
    	if result > 200:
    		return prob200(1)
    	else:
    		return result
    
    for i in range(0, 10):
    	print(prob200(1))

- aka January 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int rec(int from, int to) {
  return from == to ? from : f1() == 0 ? rec(from, (from + to) / 2) : rec((from + to) / 2, to);
}

- Anonymous January 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int f200()
{
	int sum = 0;
	for (int i = 0; i < 8; i++)
		sum += f1() << i;
	// divide for equal distribution
	sum = (int)((float)sum / 255.0f * 198.0f + 0.5f);
	return sum;

}

- Anonymous January 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Ruby:

def f1
    return Random.rand(2)
end

def f200
    # this function makes use of the property that, if you throw away
    # evenly distributed numbers, the rest of the numbers are still
    # evenly distributed

    while true do
        max = 0
        result = 0
        effect = 1

        while max < 200 do
            random_bit = f1()
            result += random_bit * effect
            max += 1 * effect

            effect *= 2
        end

        return result if result >= 1 && result <= 200

    end

end

# checking the results:
tally = Hash.new(0)
10000000.times { r = f200(); tally[r] += 1 }
tally.keys().sort().each {|k| puts "#{k}\t#{tally[k]}"}

- Kin Lum February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

for(int I = 1; I <=200;++I)
	{
		res += f1()
	}
	return res;

- anon January 04, 2016 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More