Sie sind auf Seite 1von 3

1. Is your husband a cheat?

A certain town comprises of 100 married couples. Everyone in the town lives by t
he following rule: If a husband cheats on his wife, the husband is executed as s
oon as his wife finds out about him. All the women in the town only gossip about
the husbands of other women. No woman ever tells another woman if her husband i
s cheating on her. So every woman in the town knows about all the cheating husb
ands in the town except her own. It can also be assumed that a husband remains s
ilent about his infidelity. One day, the mayor of the town announces to the whol
e town that there is at least 1 cheating husband in the town. What do you think
happens?

Answer:-
Stumped? Lets solve this methodically. Say there was only 1 cheating husband in
the town. There will be 99 women who know exactly who the cheater is. The 1 rem
aining woman, who is being cheated on, would have assumed there are no cheaters.
But now that the mayor has confirmed that there is at least one cheater, she re
alizes that her own husband must be cheating on her. So her husband gets execute
d on the day of the announcement.
Now lets assume there are 2 cheaters in the town. There will be 98 women in the
town who know who the 2 cheaters are. The 2 wives, who are being cheated on, wo
uld think that there is only 1 cheater in the town. Since neither of these 2 wo
men know that their husbands are cheaters, they both do not report their husband
s in on the day of the announcement. The next day, when the 2 women see that no
husband was executed, they realize that there could only be one explanation bo
th their husbands are cheaters. Thus, on the second day, 2 husbands are executed
.
Through induction, it can be proved that when this logic is applied to n cheatin
g husbands, they all die on the n th day after the mayors announcement.

2. 5 Pirates Fight for 100 Gold Coins


Five pirates discover a chest containing 100 gold coins. They decide to sit down
and devise a distribution strategy. The pirates are ranked based on their exper
ience (Pirate 1 to Pirate 5, where Pirate 5 is the most experienced). The most e
xperienced pirate gets to propose a plan and then all the pirates vote on it. If
at least half of the pirates agree on the plan, the gold is split according to
the proposal. If not, the most experienced pirate is thrown off the ship and thi
s process continues with the remaining pirates until a proposal is accepted. The
first priority of the pirates is to stay alive and second to maximize the gold
they get. Pirate 5 devises a plan which he knows will be accepted for sure and w
ill maximize his gold. What is his plan?
Answer:-
To understand the answer, we need to reduce this problem to only 2 pirates. So w
hat happens if there are only 2 pirates. Pirate 2 can easily propose that he get
s all the 100 gold coins. Since he constitutes 50% of the pirates, the proposal
has to be accepted leaving Pirate 1 with nothing.
Now lets look at 3 pirates situation, Pirate 3 knows that if his proposal does
not get accepted, then pirate 2 will get all the gold and pirate 1 will get noth
ing. So he decides to bribe pirate 1 with one gold coin. Pirate 1 knows that one
gold coin is better than nothing so he has to back pirate 3. Pirate 3 proposes
{pirate 1, pirate 2, pirate 3} {1, 0, 99}. Since pirate 1 and 3 will vote for it
, it will be accepted.
If there are 4 pirates, pirate 4 needs to get one more pirate to vote for his pr
oposal. Pirate 4 realizes that if he dies, pirate 2 will get nothing (according
to the proposal with 3 pirates) so he can easily bribe pirate 2 with one gold co
in to get his vote. So the distribution will be {0, 1, 0, 99}.
Smart right? Now can you figure out the distribution with 5 pirates? Lets see.
Pirate 5 needs 2 votes and he knows that if he dies, pirate 1 and 3 will get not
hing. He can easily bribe pirates 1 and 3 with one gold coin each to get their v
ote. In the end, he proposes {1, 0, 1, 0, 98}. This proposal will get accepted a
nd provide the maximum amount of gold to pirate 5.
Bonus: Think about what would happen if there are 15 pirates or 25 pirates. Post
the answer if you get it......

3. 8 Identical Balls Problem


You are given 8 identical looking balls. One of them is heavier than the rest of
the 7 (all the others weigh exactly the same). You a provided with a simple mec
hanical balance and you are restricted to only 2 uses. Find the heavier ball.
Answer:-
For convenience sake, lets name the balls 1-8. First we weigh {1,2,3} on the l
eft and {4,5,6} on the right. There are three scenarios which can arise from thi
s.
If the left side is heavier, then we know that one of 1, 2 or 3 is the heavier b
all. Weigh {1} on the left and {2} on the right. By doing this, we will know if
1 or 2 is heavier. If they balance out, then 3 is the heavier one.
If the right side is heavier, then we know that either 4, 5 or 6 is the heavier
ball. Weigh {4} on the left and {5} on the right. By doing this we will know if
4 or 5 is heavier. If they balance out, then 6 is the heavier one.
If {1,2,3} and {4,5,6} balance out, then we know either 7 or 8 is the heavier on
e. Weigh both of them to find out which one is heavier.
Confused yet? or was it too easy? This is one of the basic identical ball proble
ms. Here is a more complex problem involving 12 balls with one fake.

4. Boys and Girls


In a country where everyone wants a boy, each family continues having babies til
l they have a boy. After some time, what is the proportion of boys to girls in t
he country? (Assuming probability of having a boy or a girl is the same)
Answer:-
This is a very simple probability question in a software interview. This questio
n might be a little old to be ever asked again but it is a good warm up.
Assume there are C number of couples so there would be C boys. The number of gir
ls can be calculated by the following method.
Number of girls = 0*(Probability of 0 girls) + 1*(Probability of 1 girl) + 2*(Pr
obability of 2 girls) +
Number of girls = 0*(C*1/2) + 1*(C*1/2*1/2) + 2*(C*1/2*1/2*1/2) +
Number of girls = 0 + C/4 + 2*C/8 + 3*C/16 +
Number of girls = C
(using mathematical formulas; it becomes apparent if you just sum up the first 4
-5 terms)
The proportion of boys to girls is 1 : 1.

5. Probability of a Car Passing By


The probability of a car passing a certain intersection in a 20 minute windows i
s 0.9. What is the probability of a car passing the intersection in a 5 minute w
indow? (Assuming a constant probability throughout)

Answer:-
This is one of the basic probability question asked in a software interview. Let
s start by creating an equation. Let x be the probability of a car passing the
intersection in a 5 minute window.
Probability of a car passing in a 20 minute window = 1 (probability of no car
passing in a 20 minute window)
Probability of a car passing in a 20 minute window = 1 (1 probability of a c
ar passing in a 5 minute window)^4
0.9 = 1 (1 x)^4
(1 x)^4 = 0.1
1 x = 10^(-0.25)
x = 1 10^(-0.25) = 0.4377

Das könnte Ihnen auch gefallen