Bloomberg LP Interview Question for Software Engineer / Developers






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

Take the first number off the stream, use its absolute value as an index in the stream. If the numbers themselves are infinite, this should answer it.

- foobar November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

assume you have a memory space B stores the number you pick
1. starting from the very first from the stream A, assign B = A_1
2. move to the next one, if rand() > 1/2, assign B = A_2
3. move to the next one, if rand() > 2/3 assign B = A_3
...
k. move to the next one, if rand() > (k-1)/k assign B = A_k
...

you are guaranteed to give equal probability to each element

- colin November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 votes

The probabily with with you are keeping the number is <= k-1/k. This value increases as k increases.
For eg, 1/2 is 0.5 10/11 is 0.90..
So how can you claim equal probability ?

- acoader November 12, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So acoader, you say that when we get k = 1000, prob of keeping a number is 999/1000.

Hehehe, you just missed the big & small picture.
Prob of keeping 1st number number on iteration k is 1*(1/2)*(2/3)*(3/4)*...*((k-1)/k)=1/k

Prob of keeping nth number on it. k (n<k) is
(1/n)*((n)/(n+1))*((n+1)/(n+2))*...*((k-2)/(k-1))*(k-1)/(k) = 1/k

So, as colin said, it is guaranteed the equal probability.

- RDK December 08, 2008 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

colin is right. The algorithm he used has a name: Reservoir Sampling.

To CyberPhoenix, foobar, wiwi: Huh?

- T March 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@T
wiwi is correct..this is the way rand function works in C

- :P March 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

interesting question - i think that's the problem here is that the count in infinite.
If you assume that rand() returns a number from the infinite range 0 - 1 we an simply use that - the decimal part can be construed as index into this stream.

But theres something wrong with the above solution and I don't yet know how to improve it.
Well, rand() is a computer based function, and computers work with finite numbers (i.e. floating point is not infinite math).

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

A rand function cannot have an infinite range. When we see a number in the infinite stream, we basically can pick it or not pick it. How can we design an algorithm that the probability that a number is picked is the same for all numbers?

- Electra November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I had a similar question in Cisco interview.

The question was more or less the same.

The interviewer was satisfied with rand()

- Nachiketha November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.Generate a Gaussian Distribution float number x, between 0 and 1
2.1/x will be the random number for a infinite series.

- wiwi November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you point us to some more info on how x, which is from a finite set, becomes a pick from infinite series, when you take its reciprocal ?

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

I had similar question asked in one of interviews. If you got for rand(), the probability won't be equal for all numbers.

- Anonymous November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is simple....
It is a continuous stream, just keep track of the size of the data received.
Suppose currently you have 10 bytes, with this information you can easily pick number with equal probability. Assuming rand(10) picks number with equal probability rand(current_size) will do the trick.
int total= 0,size = 0;
while(true)
size = read(socket,buff,sizeof(buff));
total +=size;
}
rand(total); will give the number ....

- CyberPhoenix November 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 vote

Do we know the total number (count) of the numbers?

- Anonymous November 04, 2008 | 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