Salesforce

## Interview Question

## Interview Answer

9 Answers

It can be done max 2 times balancing. Divide the balls into set of 3, 3 and 2.

Measure the first set of 3 balls against the other set of 3 balls.

If both are equal measure the other 2 to find the heavier one.

If not take the set of 3 balls that was heavier and measure any 2 against each other. This will give you the heavier one. But If both are equal the third ball from that set is the heavier one.

Don't you read the question? You don't know if the special ball is heavier or lighter!

Step 1: divide 8 balls into two set, weight them

Step 2: take two from each part, if balance, then special ball is in the taken part, else it is in the balance

Step 3: take one from the balance, if balance, then special ball is in taken part, else in the balance

Step 4 replace one on the balance with other balls, if balance, then this one is special ball, else the taken one is

So, totally we need 4 times on both average and worse case using this way

Sorry, in Step 4, if not balance, the remained one is special ball

Well I guess hcb and charlie won't pass the interview :(

Let's say we have balls numbered 1 to 8.

1) Weight 1,2,3 against 4,5,6. If balanced the special ball in one of 7 or 8, goto (2), otherwise record the heavier groupe (let's say 1,2,3 is heavier) and goto (3)

2) Weight 1 against 7, if balanced the special ball is 8, else 7. 2 steps in this case

3) Weight 1,2 against 3,4, if balanced the special ball is one of 5 or 6, goto step (4), otherwise record the heavier groupe (let's say 1,2 is heavier) and goto (5)

4) Weight 1 against 5, if balanced the special ball is 6, else 5. 3 steps in this case

5) Now we consider if the special ball is heavier or lighter. If it's lighter, we check the result that 1,2,3 is heavier than 4,5,6 and 1,2 is heavier than 3,4, and we can say 4 would be the lighter ball because all other balls have the same weight. If it's the special ball is heavier, we can say the special ball is one of 1 or 2, then we can just weight 1 against 2 and find the heavier one. 3 steps in this case.

@Candidate You're wrong. Below are the different cases that could happen. This problem can be solved with best case of 2 steps.

1) OOO OOO : All same

2) O Ø : Found special ball

1) OOØ OOO : Special Ball in left side

2.1) O O : Special Ball didn't get weighed, that's the one

2.2) O Ø : Special Ball on right side

Actually, I didn't see that it can be lighter or heavier. The version I've heard is that the special ball is heavier.

a. Divide balls into sets of 3,3,2

b. Compare 3 & 3

i. If equal, compare the other 2 balls against each other.

1) If 1 is heavier then compare it any one ball in set of 6 other balls

a) If both are equal, the ball left is the special ball (lighter)

b) If the ball is heavier then it is the special ball (heavier)

ii. If not equal, take the one which is heavier and compare any 2 balls

1) If both are equal, Compare one of them with the other one left

a) If they are also equal, go to next step iii.

b) If the new ball is heavier, then it is special ball (heavier)

2) If 1 is heavier, then it is special ball (heavier)

iii. If not equal, and all the balls in other set are equal, compare any 2 balls

1. If both are equal, the ball left is the special ball (lighter)

2. If the ball is lighter, then it is the special ball (lighter)

## Add Answers or Comments

To comment on this, Sign In or Sign Up.

It needs 3 times of balancing in worst case and 2 times in best case