Probability Questions for ML Interviews

source: quanta magazine

Collection of probability questions to practise before data science/ML interview rounds.

Problem 1:

A group of 60 students is randomly split into 3 classes of equal size. All partitions are equally likely. Jack and Jill are two students belonging to that group.

What is the probability that Jack and Jill will end up in the same class ?

Solution:

19/59.

Assign random numbers from 1 to 60 to the 60 students. Let 1st group contain students with numbers 1 to 20, 2nd group with numbers 21 to 40 and the remaining group with 41 to 60.

After Jack is assigned a random number, there are 59 numbers left and Jill can be assigned 19 numbers out of 59 for her to be in the same group as Jack. Thus probability is 19/59.

For e.g. if Jack is assigned 17 then Jill can only be assigned from (1–16, 18–20), similarly if Jack is assigned 25 Jill can be assigned only (21–24, 26–40) and so on.

Problem 2:

Jack is having two coins in his hand. Out of the two coins, one is a real coin and the second one is a faulty one with Tails on both sides. He blindfolds himself to choose a random coin and tosses it in the air. The coin falls down with Tails facing upwards.

What is the probability that this tail is shown by the faulty coin ?

Solution:

Using Bayes theorem:

P(Faulty|Tails) = P(Tails|Faulty)*P(Faulty)/P(Tails)

P(Tails|Faulty) = 1

P(Faulty) = 0.5

P(Tails) = P(Tails|Faulty)P(Faulty) + P(Tails|Non-Faulty)P(Non-Faulty) = 1*0.5+0.5*0.5=0.75

P(Faulty|Tails) = 0.5/0.75 = 0.667

Problem 3:

Rob has fever and the doctor suspects it to be typhoid. To be sure, the doctor wants to conduct the test. The test results positive when the patient actually has typhoid 80% of the time. The test gives positive when the patient does not have typhoid 10% of the time.

If 1% of the population has typhoid, what is the probability that Rob has typhoid provided he tested positive?

Solution:

Using Bayes theorem:

P(Typhoid|Positive) = P(Positive|Typhoid)*P(Typhoid)/P(Positive)

P(Positive|Typhoid) = 0.8

P(Typhoid) = 0.01

P(Positive) = P(Positive|Typhoid)P(Typhoid) + P(Positive|Non-Typhoid)P(Non-Typhoid) = 0.8*0.01+0.1*0.99

P(Typhoid|Positive) = 0.0747

Problem 4:

Hospital records show that 75% of patients suffering from a disease die due to that disease. What is the probability that 4 out of the 6 randomly selected patients recover?

Solution:

Let probability of patients dying = p.

probability that patient survives = 1-p

Result = 6C4*(1-p)⁴*p² = 6C4*0.25⁴*0.75²= 0.033

Problem 5:

Ahmed is playing a lottery game where he must pick 2 numbers from 0 to 9 followed by an English alphabet (from 26-letters). He may choose the same number both times.If his ticket matches the 2 numbers and 1 letter drawn in order, he wins the grand prize and receives $10405. If just his letter matches but one or both of the numbers do not match, he wins $100. Under any other circumstance, he wins nothing.

The game costs him $5 to play.

Suppose he has chosen 04R to play.What is the expected net profit from playing this ticket?

Solution:

E(profit) = 10405*P(all matches) + 100*P(only letter matches) — 5

P(all matches) = 1/(10*10*26)

Number of ways both numbers do not match = 9*9

Number of ways only one number matches = 2C1*9

P(only letter matches) = (9*9+2C1*9)/(10*10*26) = 99/2600

E(profit) = 10405/2600 + 9900/2600–5 = $2.81

Problem 6:

In a class of 30 students, approximately what is the probability that two of the students have their birthday on the same day (defined by same day and month) (assuming it’s not a leap year)?

Solution:

Total number of ways to have the birthdays = 365³⁰

Number of ways to select 2 students = 30C2

Number of ways for the remaining 28 students to have their birthdays on different days than the 1 day when 2 students have birthday = 364*363*…*(365–28)

Probability = 30C2*364*363*…*(365–28)/365³⁰

Problem 7:

A coin of diameter 1-inches is thrown on a table covered with a grid of lines each two inches apart. What is the probability that the coin lands inside a square without touching any of the lines of the grid?

You can assume that the person throwing has no skill in throwing the coin and is throwing it randomly.

Solution:

Area of square = 4 inches²

Area of circle = pi/4 inches²

Probability = Area of circle/Area of square = pi/16

Problem 8:

Suppose you were interviewed for a technical role. 50% of the people who sat for the first interview received the call for second interview. 95% of the people who got a call for second interview felt good about their first interview. 75% of people who did not receive a second call, also felt good about their first interview.

If you felt good after your first interview, what is the probability that you will receive a second interview call?

Solution:

Using Bayes Theorem:

P(2nd call|Felt Good) = P(Felt Good|2nd call)*P(2nd call)/P(Felt Good)

P(Felt Good|2nd call) = 0.95

P(2nd call) = 0.5

P(Felt Good) = P(Felt Good|2nd call)*P(2nd call) + P(Felt Good|No 2nd call)*P(No 2nd call) = 0.95*0.5+0.75*0.5=0.85

P(2nd call|Felt Good) = 0.95*0.5/0.85 = 0.56

Problem 9:

Some test scores follow a normal distribution with a mean of 18 and a standard deviation of 6. What proportion of test takers have scored between 18 and 24?

Solution:

Distribution = N(18, 6)

Problem 10:

Suppose a life insurance company sells a $240,000 one year term life insurance policy to a 25-year old female for $210. The probability that the female survives the year is .999592.

Find the expected value of this policy for the insurance company.

Solution:

210*0.999592–240000*(1–0.999592) = $112

Problem 11:

Three friends in Seattle told you it’s rainy. Each has a probability of 1/3 of lying. What’s the probability of Seattle is rainy?

Solution:

Let the friends be A, B and C.

P(Rainy|A told rainy, B told rainy, C told rainy) = P(Rainy|A told rainy)*P(Rainy|B told rainy)*P(Rainy|C told rainy)

P(Rainy|A told rainy) = P(A told Rainy|Rainy)*P(Rainy)/P(A told Rainy)

Assuming P(Rainy) = p

P(A told Rainy|Rainy) = 2/3

P(A told Rainy) = P(A told Rainy|Rainy)*P(Rainy) + P(A told Rainy|Not Rainy)*P(Not Rainy) = 2/3*p + 1/3*(1-p) = (1+p)/3

P(Rainy|A told rainy) = 2/3*p/(2/3*p + 1/3*(1-p)) = 2p/(1+p)

P(Rainy|A told rainy, B told rainy, C told rainy) = (2p/(1+p))³

Problem 12:

A miner is trapped in a mine containing 3 doors. The first door leads to a tunnel that will take him to safety after 3 hours of travel. The second door leads to a tunnel that will return him to the mine after 5 hours of travel. The third door leads to a tunnel that will return him to the mine after 7 hours.

If we assume that the miner is at all times equally likely to choose any one of doors, what is the expected length of time until he reaches safety?

Solution:

Let the expected length of time be E.

Thus E = 1/3*(3) + 1/3*(E+5) + 1/3*(E+7) = 2/3*E+5

Solving for E: E = 15 hours

Problem 13:

Imagine there are a 100 people in line to board a plane that seats 100. The first person in line realizes he lost his boarding pass so when he boards he decides to take a random seat instead.

Every person that boards the plane after him will either take their “proper” seat, or if that seat is taken, a random seat instead.

What is the probability that the last person that boards will end up in his/her proper seat ?

Solution:

The probability of the N-th passenger getting his/her own seat is 0.5

The 1st passenger can choose either the 1st seat or any other seat k > 1.

If the 1st passenger chooses the 1st seat, then all remaining passengers take their own seats else if 1st passenger chooses seat k > 1, then all passengers from 2 to k-1 gets their own seat and k-th passenger can either choose seat 1 or any seat k’ > k.

If the k-th passenger chooses seat 1 then all passengers from k+1 to N gets their own seats. Else if he chooses seat k’ > k then again all passengers from k+1 to k’-1 gets their own seats and the k’-th passenger gets to choose either seat 1 or any seat k’’ > k’.

Thus any passenger 1 < P < N, will have 3 choices, either seat 1 or his own seat or any seat K > P.

For the last passenger N, he cannot choose any seat > N because there are no seats > N. Thus he has only option either seat 1 or his own seat. Thus probability is 0.5.

Problem 14:

Suppose that we are to be presented with n distinct prizes, in sequence. After being presented with a prize, we must immediately decide whether to accepted or to reject it and consider the next prize.

The only information we are given when deciding whether to accept a prize is the relative rank of that prize compared to ones already seen. That is, for instance, when the fifth prize is presented, we learn how it compares with the four prizes we have already seen.

Suppose that once a prize is rejected, it is lost, and that our objective is to maximize the probability of obtaining the best prize.

Assuming that all n! orderings of the prizes are equally likely, how well can we do?

Solution:

Strategy is to reject the first k prizes and then take the one that is greater than the highest prize in those k rejected prizes.

Suppose I take the i-th prize, then the probability that the i-th prize is the best prize is P(best|X=i)P(X=i)

Probability of choosing the best prize :

P(best) = P(best|X=1)P(X=1) + P(best|X=2)P(X=2) + … + P(best|X=n)P(X=n)

P(X=i) = 1/n

P(best|X=i) probability that if I choose the i-th prize then it is the best prize

It means that all prizes from k+1-th prize to i-1-th prize are smaller or equal to the highest prize in the first k prizes, then only we will select the i-th prize which is higher than the highest prize in the first k prizes.

Or in other words the best prize of the first i-1 prizes is in the first k prizes.

Thus P(best|X=i) = k/(i-1)

If i <= k, the P(best|X=i) = 0 because we reject all prizes in the first k prizes thus we do not get the best prize.

P(best) = 1/n*(1 + k/(k+1) + k/(k+2) + … + k/(n-1))

Choose k for which the above summation is highest.

Problem 15:

A bowl of spaghetti contains n strands. Thor picks two ends at random and joins them together. He does this until no ends remain. What is the expected number of spaghetti loops in the bowl?

Solution:

Let the expected number of spaghetti loops with n strands be E(n).

After picking up one end, the probability that the other end belongs to the same strand = 1/2n-1

After joining 2 ends of same strand, the number of loops = 1 and number of remaining strands = n-1

After picking up one end, the probability that the other end belongs to different strand = (2n-2)/2n-1

After joining 2 ends of different strand, the number of loops = 0 and number of remaining strands = n-1 (because 2 strands joined on one end, will form a “bigger” strand)

Then E(n) = (1/(2n-1))*(1+E(n-1)) + ((2n-2)/(2n-1))*E(n-1) = 1/(2n-1) + E(n-1)

Problem 17:

In a country in which people only want boys, every family continues to have children until they have a boy. I

f they have a girl, they have another child. If they have a boy, they stop.

What is the proportion of boys to girls in the country?

Solution:

Let the probability of having a boy/girl be 0.5. Since each family will have only one boy after infinite time, thus expected number of boys is 1.

We will have the following scenarios : B, GB, GGB, GGGB, …..

Thus expected number of girls S = 1*(1/2)²+ 2*(1/2)³+ 3*(1/2)⁴+ …..

Then (1/2)*S = (1/2)²+ (1/2)³+ (1/2)⁴+ …. = 1/2

Thus expected number of girls S = 1

Thus expected proportion is 1.

Problem 18:

Flip a fair coin repeatedly until you get two heads in a row (HH). On average, how many flips should this take?

What if we flip until we get heads followed by tails (HT)? Are the answers the same?

Solution:

Let the expected number of flips be E.

If the first toss is T, then we have E more flips, because we have wasted the first flip.

If the first 2 tosses are HT, then we have E more flips again.

If the first 2 tosses are HH, then we have only 2 flips.

E = 1/2*(1+E) + 1/4*(2+E) + 1/4*2 = 3/4*E+3/2

Solving for E = 6.

For HT:

If the 1st two flips are HT, then are done. (2)

If the first flip is T, then we have still E more number of flips to go again. (E+1)

The remaining cases are HHT, HHHT, HHHHT, ….. and so on. Observe that we cannot form recursion for this case as earlier.

Thus E = (1/4)*2 + (1/2)*(E+1) + 3*(1/2)³+ 4*(1/2)⁴+ ….

Solving for E= 4

This is intuitive because in the first case HH, if the first toss was T or first 2 tosses were HT, they were going ‘waste’ and we needed to start over. But in this case only if the 1st toss is T, it is going waste, if it is HH, then still we have obtained half of the desired sequence.

Problem 19:

There are N applicants for a secretarial position.

The applicants are interviewed in random order, and you must accept or reject a candidate immediately after interviewing them. After you reject someone, there’s no way to bring them back.

There is only one position available, so as soon as you accept a candidate you are done.

What strategy should you pursue in order to maximize the likelihood of hiring the best candidate out of the N applicants?

Solution:

Same as Problem 14.

Problem 20:

Let’s play a game. I pick a probability distribution on the real line. You know nothing about it, except that it’s everywhere nonzero.

Using my probability distribution, I generate two distinct numbers.

Then I flip a fair coin to determine which number to show you — call it A, and call the other number B.

Then I ask you whether A < B or B < A ? Certainly, you can just guess.

And you’d have a 50 percent chance of winning. But is there a strategy to do better?

Solution:

Strategy is to pick a random number C and if A > C then predict that A > B, else if A < C then predict A < B.

Let the 2 numbers picked be X and Y.

Following are the possible cases with X, Y and C:

{X<Y<C, X<C<Y, C<X<Y, Y<X<C, Y<C<X, C<Y<X}

P(wins) = P(wins, X<Y<C) + P(wins, X<C<Y) + … + P(wins, C<Y<X)

P(wins) = P(wins|X<Y<C)*P(X<Y<C) + P(wins|X<C<Y)*P(X<C<Y) + …

P(wins|X<Y<C) = 0.5 because if the number picked after toss is X then X<Y<C is a success because X < C and X < Y strategy works. But if the number picked after toss is Y then it is failure because Y < C but Y > X strategy fails.

Similarly P(wins|C<X<Y), P(wins|Y<X<C), P(wins|C<Y<X) are all 0.5 by same reasoning as above.

But P(wins|X<C<Y) = 1 because if the number picked after toss is X, then it is success since X < C and X < Y strategy works. If the number picked after toss is Y, then it is also a success since Y > C and Y > X strategy works.

Similarly P(wins|Y<C<X) = 1.

P(wins) = 0.5*[P(X<Y<C) + P(C<X<Y) + P(Y<X<C) + P(C<Y<X)] + 1.0*[P(X<C<Y) + P(Y<C<X)]

Rewriting the above:

P(wins) = 0.5*[P(X<Y<C) + P(C<X<Y) + P(Y<X<C) + P(C<Y<X) + P(X<C<Y) + P(Y<C<X)] + 0.5*[P(X<C<Y) + P(Y<C<X)]

P(X<Y<C) + P(C<X<Y) + P(X<C<Y) = P(X<Y)

P(C<Y<X) + P(C>X>Y) + P(Y<C<X) = P(X>Y)

P(wins) = 0.5*[P(X<Y) + P(X>Y)] + 0.5*[P(X<C<Y) + P(Y<C<X)]

P(X<Y) + P(X>Y) = 1

P(wins) = 0.5 + 0.5*[P(X<C<Y) + P(Y<C<X)] > 0.5

Thus probability of winning is greater than 1/2.

Problem 21:

The probability of a car passing a certain intersection in a 20 minute windows is 0.9. What is the probability of a car passing the intersection in a 5 minute window?

Solution:

Probability of not seeing the car in time X to X+20 = Probability of not seeing the car in time X to X+5 and Probability of not seeing the car in time X+5 to X+10 and Probability of not seeing the car in time X+10 to X+15 and Probability of not seeing the car in time X+15 to X+20

Assuming that each interval [X, X+5], [X+5, X+10], [X+10, X+15] and [X+15, X+20] are equally probable (uniform distribution) to see a car.

Let y be the probability of not seeing the car in the 5 minute window.

Then probability of not seeing the car in time X to X+20 = y⁴

Thus probability of seeing the car in time X to X+20 = 0.9 = 1-y⁴

Thus y = 0.1^(0.25)

Probability of seeing the car in the 5 minute window = 1-y = 0.437

Problem 22:

Let’s say A keep tossing a fair coin, until he get 2 consecutive heads, define X to be the number of tosses for this process; B keep tossing another fair coin, until he get 3 consecutive heads, define Y to be the number of the tosses for this process. Calculate P{X>Y}

Solution:

P(X>Y) = [P(X=4, Y=3)] + [P(X=5, Y=3) + P(X=5, Y=4)] + [P(X=6, Y=3) + P(X=6, Y=4) + P(X=6, Y=5)] + ….

Since minimum number of tosses for 3 H’s is 3, thus for X > Y, X must start from 4.

We can write P(X=i, Y=j) = P(X=i)*P(Y=j) since they are independent

Let Q(Y=k) = P(Y=3) + P(Y=4) + … + P(Y=k) is the cumulative probability

Then P(X>Y) = P(X=4)*Q(Y=3) + P(X=5)*Q(Y=4) + P(X=6)*Q(Y=5) + …..

P(X=n) = G(n)*(1/2^n) where G(n) = G(n-1)+G(n-2)

P(Y=n) = H(n)*(1/2^n) where H(n) = H(n-1)+H(n-2)+H(n-3)

Solving for the above we get P(X>Y) = 0.2125

Problem 23:

Suppose n fair 6-sided dice are rolled simultaneously. What is the expected value of the score on the highest valued die?

Solution:

For K=1 to 6

Number of ways at-least one die comes up with K (among possible values of 1 to K) = Number of ways n die comes up with values between 1 to K — Number of ways n die comes up with values between 1 to K-1

Number of ways at-least one die comes up with K (among possible values of 1 to K) = K^n — (K-1)^n

P(K) = (K^n — (K-1)^n)/6^n

E = P(1) + 2*P(2) + 3*P(3) + 4*P(4) + 5*P(5) + 6*P(6)

Problem 24:

A stick is broken into 3 pieces, by randomly choosing two points along its unit length. What is the probability that the 3 parts form a triangle ?

Solution:

The 3 parts can form a triangle only if the longest part is less than half the length of the stick. This is due to the property that for the 3 parts X, Y and Z it must satisfy X+Y > Z, X+Z > Y and Y+Z > X.

If the length of a part is greater than half the length of rod then the remaining 2 parts will be less than half the length.

Thus there can be 4 possible cases (exclusive):

  • 1st part is greater than half length
  • 2nd part is greater than half length
  • 3rd part is greater than half length
  • All parts are less than half length

All 4 cases are equally probable and only in the last case a triangle can be formed. Thus probability is 1/4 = 0.25.

Problem 25:

There are n letters and n envelopes. Your servant puts the letters randomly in the envelopes so that each letter is in one envelope and all envelopes have exactly one letter. (Effectively a random permutation of n numbers chosen uniformly).

Calculate the expected number of envelopes with correct letter inside them.

Solution:

If we write down all permutations of 1 to N, then each number has equal probability of occuring in all positions. Thus 1 occurs in 1st position 1/N times, 2 occurs in 2nd position in 1/N times and so on. Thus each number has 1/N probablity of occuring in its corresponding position. Probability that a number do not occur in its respective position is thus (N-1)/N

E = E(1) + E(2) + … + E(N)

E(i) is the expectation that i-th letter occurs in its respective position

E(i) = 1*(1/N) + 0*(N-1/N) = 1/N

E = 1

Problem 26:

A soda company is holding a contest where everyone who collects one each of N different coupons wins some prize. You get a coupon with each purchase of a soda, and each coupon is equally likely.

What’s the expected number of soda bottles you have to buy in order to collect all the coupons?

Solution:

Let the N coupons when ordered by the time when it was obtained for the 1st time be C1, C2, …, CN. i.e. Ci was obtained before any Ci+1.

Let Xi denote the random variable of the number of trials after Ci is obtained and before Ci+1 is obtained.

E(Xi) is the expected number of trials for the i-th interval.

E = E(X0) + E(X1) + … + E(XN-1)

Probability of observing a success in the i-th interval = 1-Probability of not getting the (i+1)-th coupon = 1-probability of getting a coupon from the first i coupons = 1-i/N

P(Xi) = 1-i/N

E(Xi) = 1/P(Xi) = N/(N-i)

E = N*[1 + 1/2 + 1/3 + … + 1/N] approximates to N*logN when N is large.

Problem 27:

You are in possession of n pairs of socks (hence a total of 2n socks) ranging in shades of grey, labeled from 1 (white) to n (black).

Take the socks blindly from a drawer and pair them at random.

What is the probability that they are paired so that the colors of any pair differ by at most 1?

Solution:

Let us denote the first socks in each pair by {X1, X2, …, Xn} and the 2nd socks in the corresponding pairs as {Y1, Y2, …, Yn}.

Let F(n) be the number of ways to pair the socks such that Xi can be paired only with Yi or Yi-1 or Yi+1.

If we pair Xi with Yi, then we have n-1 pairs left i.e. F(n-1)

If we pair Xi with Yi-1 then Yi can only pair with Xi-1, similarly if we pair Xi with Yi+1, then we can pair Yi with only Xi+1, thus we have n-2 pairs remaining for these 2 cases.

F(n) = F(n-1) + 2*F(n-2)

Number of all possible ways to select the pairs G = (2n)!/(2n*n!)

Because there are 2n socks and for each permutation of 2n socks we create n pairs by taking the consecutive socks.

For each pair (A, B) it is same as (B, A) thus we divide by 2 and there are n pairs thus we divide by 2n. Also if there are 2 pairs (A, B) and (C,D) then it doesn’t matter it which order I select these 2 pairs thus divide by n!

P(n) = F(n)/G

Problem 28:

You have 50 red marbles, 50 blue marbles and 2 jars. One of the jars is chosen at random and then one marble will be chosen from that jar at random. How would you maximize the chance of drawing a red marble? What is the probability of doing so?

Solution:

Put 1 red marble in one jar and 49 red marbles + 50 blue marbles in another. Probability of selecting red marble = 0.5*1+0.5*49/99 = 0.75.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deep Feedforward Networks

INTRODUCTION TO LOGISTIC REGRESSION

Delving into the rapid rise of NLP

Tutorials: How to create a simple chatbot with Dialogflow (No coding required!) — Part 2

Machine UnLearning: What it is All About?

How to overcome from local minima problem in Gradient Descent.

Insights from EMNLP 2017

What is an Artificial Neural Network…?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhijit Mondal

Abhijit Mondal

Engineer

More from Medium

Enabling effective data science through lifecycle rules in SageMaker Studio

What does an ML Manager do?

Intro to Hadoop and It’s Core Components

Machine Learning for Developers

Ameca Humanoid Robot AI Platform at CES2022.