Bloomberg LP Interview Question
Software Engineer / Developers1st person starts the process by taking up any random number.
he adds his salary to this number and passes it to 2nd.
2nd adds his salary to this figure and passes it to 3rd
3rd adds his salary to this figure and passes it to 1st
1st subtracts the random number he had selected and then gets the total sum of all their salaries.... and gets the average.
When 2nd adds his salary and passes it to the 3rd one everyone knows his salary and the same with the 3rd one.
So at each step everyone adds his salary and a random number and the same thing happens and after one circle every one subtract their random number and in the end they get total salary sum and the average.
No, consider salaries (100,110,91)
1st one adds random number to his salary, 12345 + 100 -> 12445
2nd one doesn't know the random number and adds his salary, 12555
third one also adds his salary -> 12646
Now 1st one subtracts random number -> 12646 - 12345 = 301
average = 301/3 = 101.333
If they are saying it out loud so everyone in the table hears once the 2nd one adds his salary and says the new number first guy will know 2nd guy's salary immediately. If they are whispering to each other's ears than you would be right.
A more clear rundown...
First guy says: Rand + salary1
Second guy says: Rand + salary1 + salary2
At this point first guy already know the random number and his salary. Now he can find the second guy's salary easily.
Third guy says: Rand + salary1 + salary2 + salary3
Now the first guy know everyone's salary and second guy knows third guy's salary.
Josh's solution won't work as well unless you assume that they forget the past numbers they heard. When the first guy subtracts his random number the 2nd guy will know his random number. He already knows salary1 + random number so now he knows first guy's salary.
my solution will give an approximate solution. ask each person to give a range of their salaries (lower limit, upper limit) where upper limit-lower limit = constant for all persons and the exact salary should not be the average of the two limits.
suppose the values are (x1,y1),(x2,y2)and (x3,y3) where y1-x1= y2-x2= y3-x3= d (say).
step 1: find Xmin = min (x1,x2,x3) and Ymax = max (y1,y2,y3).
step 2: find P = (Xmin+Ymax)/2
step 3: find q1 = (x1+y1)/2, q2 =(x2+y2)/2, q3 =(x3+y3)/2
step 4: find Q1 = min(q1,q2,q3), Q2= max(q1,q2,q3), Q =(Q1+Q2)/2
step 5: find A = (P+Q)/2
The solution will be fairly accurate for small values of d.
why not take this algo..
process 1 adds a random number 'x' and passes the token to process 2,
process 2 adds its value and a random number 'y' and passes to process3.
process3 adds its value and passes to process1
process1 subtracts x from total and gives to process2.
process2 subtracts y from total and divides by 3.
:)...no one knows what others salaries are...
If you are assuming that they are whispering to each other (3rd person doesn't hear) just the first person using the random number is enough. The other 2 random numbers are unnecessary.
1st person starts the process by taking up any random number.
- VineetG November 22, 2010he adds his salary to this number and passes it to 2nd.
2nd adds his salary to this figure and passes it to 3rd
3rd adds his salary to this figure and passes it to 1st
1st subtracts the random number he had selected and then gets the total sum of all their salaries.... and gets the average.