If you have 9 balls and one is heavier than the rest how to pinpoint the heavy one by using the balance only once?

Read more: http://wiki.answers.com/Q/If_you_have_9_balls_and_one_is_heavier_than_the_rest_how_to_pinpoint_the_heavy_one_by_using_the_balance_only_once#ixzz26piPvdP9

The funny thing is in almost 4 decades of writing software, either professionally or otherwise, I have yet to be asked to solve this kind of a problem by writing some software.

And “Yes” I realize this is supposed to illuminate something about the person who responds… but what difference does it makes as to whether we use the scale 2 or 3 times or 50 times – none of this has anything to do with computers other than whether or not the person who responds has ever spent any time thinking about this sort of problem.

It is still entirely possible to respond correctly to this type of logic problem and still manage to write crappy code.

It is also still possible to respond correctly to this type of logic problem and still manage to work for someone who cannot tell when he or she is asking for crappy code to be produced.

Also the correct algorithm for all problems of this type is to reduce the number of objects into 3 or fewer distinct groups and then use the scale once less than the number of groups.

If one’s job had everything to do with having to find the heaviest thing from a number of things then that person would quickly determine the fastest approach and use it – the rest is without meaning.

Also, if you think you can make assumptions with software to reduce the problem of either producing the code or testing it then by all means, assume away – assumptions draw bugs and defects – your customers will surely not know about your assumptions and they will find your defects for you – you have to hope they will continue being your customers during the process. **I would rather be overly careful than stupidly optimal any day of the week !!!**

1 – Split the balls into three groups of three balls each.

2 – **First use of the scale:** Weigh any two groups against each other.

3 – If the groups weigh the same then the heavier ball is in the third group, otherwise it is in the group that weighs more.

4 – We now know that the heavy ball is one of three.

5 – **Second use of the scale:** Using the group of balls that we know contains the heavy ball, weight any two balls against each other.

6 – If one of the balls is heavier then we have our answer. If the balls weigh the same then the third ball is the heavy one and we again have our answer.

Read more: http://wiki.answers.com/Q/If_you_have_9_balls_and_one_is_heavier_than_the_rest_how_to_pinpoint_the_heavy_one_by_using_the_balance_only_once#ixzz26pi5EANN

Okay, so now I know how to solve this silly problem – does this suddenly make me a better programmer than I was before ?!?

I mean, really !!!!

BTW – this algorithm given as the acceptable response does nothing with the fact that none of the balls may be heavier since they may still all be the same weight. You can call this an edge condition, if you wish.

### Like this:

Like Loading...