Facebook Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public static int getRan(Node m)
	{
		int range=1;
		Random random=new Random();
		int ret=(Integer)m.getValue();
		while(m.next!=null)
		{
			m=m.next;
			range++;
			if(random.nextInt(range)==0)
			{
				ret=(Integer)m.getValue();
			}
		}
		return ret;
	}

- Vincent August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, it's right.
Suppose there are a stream of bytes "abc",
when you read "a", ret = "a" with probility = 1,
when you read "b", ret = "b" with probility = 1/2, so the probility of ret = "a" is 1/2 now,
when you read "c", ret = "c" with probility = 1/3, so the probility of ret = "b" is (1 - 1/3) * 1/2 = 1/3, and the probility of ret = "a" is (1 - 1/3 - 1/3) * 1 = 1/3.
and so on.

- fancysimon August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yep, this is correct.

The question says you only have one byte of storage space, so if you followed that strictly, you wouldn't be able to keep a variable like "range". Since it's impossible to solve the problem without having a counter like that, however, it's perfectly appropriate to ignore that requirement.

- eugene.yarovoi August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

reservoir sampling

- jiangok2006 August 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I feel it is wrong. If we repeat running the algorim twards the same stream, statistitcally we should see "a" in 50% of the attempts.

- z September 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution!

- neo October 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's also what I thought, just it said "only storage for one byte" ;). But we need a counter...

- FBNerd February 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It cannot have probability of 'a' selection as fancysimon said 1 because nextInt(2) would return 0 or 1 which means probability of 'a' is 1/2. Also, if we move the increment range++; after the check then we will always select the first byte which also wrong.
On the other hand, if we have only one byte "a" it should return that byte for sure. So, I think this solution is doing some assumptions.

- Zero2 December 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Reservoir Sampling.

- Anonymous August 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

You need to be able to store how many elements you've seen so far (which presumably can't fit into a byte) for this problem to be possible.

- eugene.yarovoi August 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes.

- Anonymous August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Lol

- ACP Pradyuman November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think, firstly, you should know how many bytes of this stream.
For example, if there are 5 bytes in this stream.
Then, for the first byte:
You can use:
if ( (int)(Math.Random()*5)==0 ){
Store this byte;
return this byte;
}
for the second byte:
if ( (int) (Math.Random()*4) ==0 ){
Store this byte;
return this byte;
}
.........
for the kth byte of n byte:
if ( (int) (Math.Random()*(n-k+1)) ==0 ){
Store this byte;
return this byte;
}
.....
every byte have the same probability

- Chengyun Zuo August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's a stream, so you don't know that.

- eugene.yarovoi August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It does say after processing those bytes which means you can count the number of bytes.

- Zero2 December 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

tag

- jiangok2006 August 14, 2012 | 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