Cracking The Coding Interview/Q 6.1

From Software Engineers Wiki
Jump to: navigation, search

You have 20 bottles of pills. 19 bottles have 1.0 gram pills, but one has pills of weight 1.1 grams. Given a scale that provides an exact measurement, how would you find the heavy bottle? You can only use the scale once.

Answer

  1. Assign unique number from 1 to 20 to each bottle.
  2. From a bottle number N, take N number of pills.
  3. Weight all the pills once.
  4. If all pills weighted 1.0 gram, they would weight 210 grams. If the bottle N has 1.1 gram pills, it will weight 210 + N * 0.1 grams.
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox