Digital Ocean Interview Question for IC3s


Team: Compute
Country: United States
Interview Type: Phone Interview




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

This seems like a "Reservoir sampling" problem. Select K sample from a stream of samples.
+ Fill a buffer of size K with first K samples from stream.
+ Continue processing stream, and create a random number from 0 to current index for stream.
+ If number is in range [0,K] then overwrite current sample at this random index with current item from stream.

vector<int> reservoirSampling( const vector<int>& stream, int k )
{
	vector<int> result(k);
	
	// copy first k elements from stream as it is
	for( int i=0; i<k; ++i)
	{
		result[i] = stream[i];
	}
	
	srand( time(0) );
	
	for(; i<stream.size(); ++i )
	{
		// pick a random index from 0 to i inclusive
		int index = rand() % (i+1);
		
		// overwrite sample is randomly chosen index is between 0 and k.
		if( index < k)
		{
			result[index] = stream[i];
		}
	}
	
	return result;
}

- mr.robot.fun.society November 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As soon as integers come, I'd collect them in chunk of N (whose size is much smaller than the all stream) and pick one number by each chunk.

- kususe November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select the first with P(first) = 1 and keep that as selected number
if the 2nd arrives select that with P(2nd) = 0.5, run the experiment (e.g. if(random(2) == 0)) and take that as selected number accordingly, otherwise keep the other
if the 3rd arrives, select that with P(3rd) = 0.333, e.g. if(random(3)==0) ...
etc...
you need to prove uniformity, given your random(int max) is uniform:
P(selected = first) = 1/1 - (1/2 + 1/3 + ... + 1/n) = 1/n
P(selected = 2nd) = 1/2 - (1/3 + 1/4 + ... + 1/n) = 1/n
P(selected = k) = 1/k - (1/(k+1) + 1/(k+2) + ... + 1/n) = 1/n

void selectFromStream(intstream s) {
	size_t read_so_far = 0;
	int selected = numeric_limits<int>::min(); // suppose min never occures in the stream
	while(!s.is_eof()) {
		read_so_far++;
		if(random(read_so_far) == 0) { // suppose random is uniform, returns [0, max)
			selected = s.get_current();
		}
		s.next();
	}
	return selected;
}

- Chris November 04, 2017 | 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