This is a story about an interview question.
The technical guy asked this question.
“There are three colleagues who don’t want to reveal their salary to anyone, but they want to calculate the average of their salary. They have a calculator. How can u help them to find the average salary?”
Well this is good question. I thought for some time. Suddenly my mind remembered one of problem which I had solved earlier.
That puzzle goes like this.
“A car can travel at a speed of 63 miles/hour in straight road, 56 miles/ hour in uphill and 72 miles/hour in downhill. This car starts from A and reaches B after 4 hours. Then starts from B towards A and reaches A after 4 hours 40 minutes. What’s the distance between A and B?”
This problem I had solved like this…
Let Xu be the total uphill distance from A to B( which becomes downhill on the return trip) Xd be the total downhill distance from A to B( which becomes uphill on the return trip), and Xs be the total distance of straight road between A and B in miles.
Then problem reduces to form.
Xu / 56 + Xs / 63+ Xd / 72 = 4 hours
Xd / 56 + Xs / 63+ Xu / 72 = 4 hours 40 minutes.
Adding these two equations, will give
(Xu+Xd)(1/56+1/72) + 2Xs/63 = 8 hours and 40 minutes.
That is 1/63 – 1/56 = 1/72 – 1/63.
Hence (1/56+1/72) = 2/63
2/63(Xu+Xd+ Xs) = 8 hours 40 minutes.
Which gives the (Xu+Xd+ Xs), that is distance between A and B. Pretty simple isn’t it?
Lets say the salary of people be X1, X2, X3
I will choose three different numbers in HP lets say a,b,c. I will tell 1st and the 3rd person a and c, for the 2nd person b. The logic goes like this. 1st person will divide his salary by ‘a’ in the calculator and give it to 2nd person.
Sum in the calculator now = X1/a
Sum in the calculator now = X1/a + (2/b* X2)
Sum in the calculator now = X1/a + (2/b* X2) + (X3*(1/a+1/c))
Sum in the calculator now = X1/a + (2/b* X2) + (X3*(1/a+1/c))+ X1/c
(X1+ X2+X3)*2/b
I wasn’t selected in that interview. But im still proud of this answer as this answer came as out of box solution.
Sum in calculator = X1+ Y
Let other second and third person add their actual salary and pass it to the first.
Sum in calculator = X1+ Y+ X2+X3
First person who is aware of Y will now remove Y from this sum and divide by three to get the average of their salary.
I remain
The J.
1 comment:
Awesome solution dude :)
Cheers,
Rosh
(http://roshabraham.blogsome.com)
Post a Comment