N Friends

Brain Teasers with Coding Study

Few companies ask the below brain teaser to the candidates during the interview.

You are about to get on a plane to Mumbai, you want to know whether you have to bring an umbrella or not. You call 3 of your random friends in Mumbai and ask each one of them if it is raining. The probability that your any friend is telling the truth is 2/3 and the probability that (s)he is playing a prank on you by lying is 1/3. If all three of them tell that it is raining, then what is the probability that it is actually raining in Mumbai.

But what if it is a coding challenge:

You are about to get on a plane to Mumbai, you want to know whether you have to bring an umbrella or not. You call N of your random friends in Mumbai and ask each one of them if it is raining. The probability that your any friend is telling the truth is 2/3 and the probability that (s)he is playing a prank on you by lying is 1/3. If all N of them tell that it is raining, then what is the probability that it is actually raining in Mumbai.

Can you write a program that calculates the probability that it is actually raining in Mumbai.

The coding solution was originally published in my book “Brain Teasers with Coding For Data Scientist”. Puzzles from that book are published on this link. Also, check my coding puzzle books “Brain Teasers with Coding For Data Scientist 2: 9 New Computational Puzzles” and “Puzzles with Coding: Puzzles for Everyone” and “Computational Puzzles To Flex Your Brain: 50 original puzzles to sharpen computing mind and mathematical skills at Amazon. For purchasing my books or related queries, feel free to contact me. In these books, you will find interesting brain teasers those are suitable for coders as well as non-coders. Coding solutions are also provided with Python code. So, grab a copy of those books and jump into the world of fascinating and exciting brain teasers.

Solution:

You only require one of the friends to be telling the truth.

The probability that at least one of them is telling the truth will be =

1 – (Probability that all of them Lied)

The probability that one of them lied is 1/3

So, the probability that all N lied is 1/3 * 1/3 * 1/3 * …* 1/3 (N times multiplication of 1/3) = 1/(3^N)

So, now the probability that at least one of them told the truth is or there is a raining is = 1 – (1/(3^N))

Coding Solution:

# Probability of Raining
import math
# N Number of friends
N = int(input("Enter N:"))
if N < 1:
    print("N should be greater than 0")
    exit()
# P Probability = 1 - (Probability that all of them Lied)
P = 1 - (1/pow(3,N))
print('Possibility of Raining', P*100,'%')

Note : (Keeping non-coder readers in mind, code optimization may not be fully utilized.)

Test Output 1:
Enter N:1
Possibility of Raining 66.66666666666667 %

Test Output 2:
Enter N:2
Possibility of Raining 88.88888888888889 %

Test Output 3:
Enter N:3
Possibility of Raining 96.2962962962963 %

Test Output 4:
Enter N:5
Possibility of Raining 99.58847736625515 %