Imagine you have a bag with 3 red marbles and 2 blue marbles. Probability is just your way of answering: 'If I grab one without looking, how likely is it to be red?' You count the outcomes you WANT, divide by ALL possible outcomes, and that fraction is your probability. Every casino game, weather forecast, and spam filter runs on exactly this idea — just with more marbles.
Probability questions show up in almost every tech and finance aptitude round — not because companies want mathematicians, but because these problems reveal how you reason under uncertainty. When an interviewer asks 'what are the odds of drawing two aces from a shuffled deck?', they're watching whether you break a problem into smaller pieces, whether you remember to account for replacement vs. no-replacement, and whether you catch your own errors. These are the same mental habits that make a good engineer.
The frustrating part is that most candidates memorise formulas without understanding the logic behind them. So when a question is slightly rephrased — a bag becomes a box, marbles become cards — they freeze. The formula didn't break; their mental model was never solid in the first place. This article fixes that by building probability from the ground up: what it means, why the rules are what they are, and when each rule applies.
By the end of this article you'll be able to set up any classic probability problem from scratch without hunting for the right formula, spot the three most common traps that cost candidates marks, and explain your reasoning out loud — which is exactly what interviewers are listening for.
The Foundation: What Probability Actually Means (and Why It's a Fraction)
Probability is a number between 0 and 1 that measures how likely an event is. A probability of 0 means impossible. A probability of 1 means certain. Everything else lives in between.
The core formula is: P(Event) = (Number of favourable outcomes) / (Total number of equally likely outcomes).
The word 'equally likely' is doing serious heavy lifting there. If you roll a fair six-sided die, each face has the same chance — that's what makes the formula valid. If the die were weighted, the formula breaks immediately. Always ask: are my outcomes truly equally likely?
There are two fundamental rules everything else builds on. The Addition Rule handles OR situations: if you want event A or event B, you add their probabilities but subtract any overlap to avoid double-counting. The Multiplication Rule handles AND situations: if you want event A and then event B, you multiply — but only if the events are independent of each other. Understanding when to add and when to multiply is the single biggest skill in aptitude probability.
basic_probability_foundation.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# === Basic Probability: Die Roll and Coin Toss Examples ===# Run this to see the core formula in action with real numbers.defprobability(favourable_outcomes, total_outcomes):
"""Returns probability as a decimal and a readable fraction string."""if total_outcomes == 0:
raiseValueError("Total outcomes cannot be zero — that's not a valid sample space.")
prob = favourable_outcomes / total_outcomes
return prob, f"{favourable_outcomes}/{total_outcomes}"# --- Example 1: Rolling a fair six-sided die ---# Event: getting an even number (2, 4, or 6)
total_die_faces = 6
even_faces = [2, 4, 6] # these are our favourable outcomes
favourable_even = len(even_faces)
prob_decimal, prob_fraction = probability(favourable_even, total_die_faces)
print(f"P(rolling an even number) = {prob_fraction} = {prob_decimal:.4f}")
# --- Example 2: Drawing a card — Addition Rule (OR) ---# Event: drawing a King OR a Heart from a standard 52-card deck# Kings: 4 cards. Hearts: 13 cards. BUT King of Hearts is in both — avoid double-counting!
total_cards = 52
kings = 4
hearts = 13
king_of_hearts = 1# the overlap between the two groups# P(King OR Heart) = P(King) + P(Heart) - P(King AND Heart)
favourable_king_or_heart = kings + hearts - king_of_hearts # = 16
prob_king_or_heart = favourable_king_or_heart / total_cards
print(f"\nP(King OR Heart) = {favourable_king_or_heart}/{total_cards} = {prob_king_or_heart:.4f}")
# --- Example 3: Tossing two fair coins — Multiplication Rule (AND) ---# Event: getting Heads on both coins# The coins are independent — first coin result does NOT affect the second
p_heads_coin1 = 1 / 2# P(Heads) on a fair coin
p_heads_coin2 = 1 / 2# same, independent event
p_both_heads = p_heads_coin1 * p_heads_coin2 # multiply because AND + independentprint(f"\nP(Heads AND Heads) = 1/2 × 1/2 = {p_both_heads:.4f}")
# Sanity check: list every possible outcome
all_outcomes = [(c1, c2) for c1 in ['H', 'T'] for c2 in ['H', 'T']]
favourable = [o for o in all_outcomes if o == ('H', 'H')]
print(f"All outcomes: {all_outcomes}")
print(f"Favourable outcomes: {favourable}")
print(f"P(HH) by counting = {len(favourable)}/{len(all_outcomes)} = {len(favourable)/len(all_outcomes):.4f}")
Output
P(rolling an even number) = 3/6 = 0.5000
P(King OR Heart) = 16/52 = 0.3077
P(Heads AND Heads) = 1/2 × 1/2 = 0.2500
All outcomes: [('H', 'H'), ('H', 'T'), ('T', 'H'), ('T', 'T')]
Favourable outcomes: [('H', 'H')]
P(HH) by counting = 1/4 = 0.2500
Pro Tip: Always enumerate small sample spaces
For any problem with fewer than ~20 total outcomes, list every single possibility before applying a formula. It takes 30 seconds and instantly confirms whether your formula gave the right answer. Interviewers love candidates who sanity-check their own work.
Production Insight
In production, assuming equally likely outcomes when they aren't leads to incorrect risk assessments.
For example, assuming all traffic patterns are equally likely when load is skewed causes wrong capacity planning.
Rule: Always verify the 'equally likely' assumption before applying the formula.
Key Takeaway
Probability = favourable / total equally likely outcomes.
OR adds, AND multiplies (with dependency adjustments).
Enumerate small sample spaces to validate your reasoning.
Replacement vs. No Replacement — The Question That Trips Everyone Up
Here's the single most common source of wrong answers in probability aptitude questions: forgetting whether the item goes back into the pool after each draw.
With replacement, every draw is independent. You draw a card, note it, put it back, shuffle. The deck is always 52 cards. The second draw has no memory of the first.
Without replacement, the draws are dependent. You draw a card and keep it. Now the deck has 51 cards — and crucially, the composition of the deck has changed. The probability for the second draw must be recalculated based on what's left.
This is called conditional probability. The probability of event B given that event A has already happened is written P(B | A), pronounced 'P of B given A'. The multiplication rule for dependent events becomes: P(A and B) = P(A) × P(B | A).
A classic interview question: 'What's the probability of drawing two Aces in a row from a shuffled deck, without replacement?' The answer is (4/52) × (3/51) — four aces available first, then only three aces left in a 51-card deck. Getting this right immediately shows the interviewer you understand dependency.
replacement_vs_no_replacement.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# === Replacement vs No-Replacement: Cards and Marbles ===import math
# --- Scenario: Drawing 2 Aces from a 52-card deck ---# WITH REPLACEMENT: deck is reset after each draw — draws are independent
p_first_ace_with = 4 / 52# 4 aces in 52 cards
p_second_ace_with = 4 / 52# deck is restored — still 4 aces in 52
p_two_aces_with_replacement = p_first_ace_with * p_second_ace_with
print("=== WITH REPLACEMENT ===")
print(f"P(1st Ace) = 4/52 = {p_first_ace_with:.6f}")
print(f"P(2nd Ace) = 4/52 = {p_second_ace_with:.6f} (deck unchanged)")
print(f"P(Two Aces) = {p_two_aces_with_replacement:.6f}")
print()
# WITHOUT REPLACEMENT: first ace is removed — second draw sees 51 cards, 3 aces
p_first_ace_without = 4 / 52# 4 aces in 52 cards
p_second_ace_without = 3 / 51# 3 aces remain in 51 remaining cards — KEY CHANGE
p_two_aces_no_replacement = p_first_ace_without * p_second_ace_without
print("=== WITHOUT REPLACEMENT ===")
print(f"P(1st Ace) = 4/52 = {p_first_ace_without:.6f}")
print(f"P(2nd Ace | 1st was Ace) = 3/51 = {p_second_ace_without:.6f} (deck shrinks!)")
print(f"P(Two Aces) = {p_two_aces_no_replacement:.6f}")
print()
print(f"Difference between methods: {abs(p_two_aces_with_replacement - p_two_aces_no_replacement):.6f}")
print("(Ignoring replacement makes you ~14% over-confident in this case)")
print()
# --- Marble Bag Problem (classic aptitude style) ---# Bag contains: 5 red, 3 blue, 2 green marbles (10 total)# Q: P(drawing red then blue) WITHOUT replacement?
total_marbles = 10
red_marbles = 5
blue_marbles = 3
p_red_first = red_marbles / total_marbles # 5/10
p_blue_second_given_red = blue_marbles / (total_marbles - 1) # 3/9 — one red is gone
p_red_then_blue = p_red_first * p_blue_second_given_red
print("=== MARBLE BAG (No Replacement) ===")
print(f"P(Red first) = {red_marbles}/{total_marbles} = {p_red_first:.4f}")
print(f"P(Blue second | Red) = {blue_marbles}/{total_marbles-1} = {p_blue_second_given_red:.4f}")
print(f"P(Red then Blue) = {p_red_then_blue:.4f}")
print(f"As a fraction = {red_marbles*blue_marbles}/{total_marbles*(total_marbles-1)} = {red_marbles*blue_marbles}/90")
(Ignoring replacement makes you ~14% over-confident in this case)
=== MARBLE BAG (No Replacement) ===
P(Red first) = 5/10 = 0.5000
P(Blue second | Red) = 3/9 = 0.3333
P(Red then Blue) = 0.1667
As a fraction = 15/90 = 15/90
Watch Out: The 'restored deck' assumption
Unless a problem explicitly says 'with replacement' or 'the card is put back', always assume no replacement. Real-world draws — lottery balls, cards dealt in poker, hiring from a candidate pool — are almost always without replacement. Assuming independence when there's actually dependence is the #1 error in aptitude exams.
Production Insight
Forgetting no-replacement in dependency calculations causes overestimation of success probabilities in retry logic.
Example: A system retries a database connection assuming independence, but after a failure the pool has fewer connections, changing the odds.
Rule: After each dependent event, reduce both numerator and denominator.
Key Takeaway
With replacement: independent, same denominator.
Without replacement: dependent, reduce denominator and numerator.
This is the #1 trap in probability problems.
Combinatorics + Probability: Solving 'At Least One' and Multi-Event Problems
Once you're comfortable with single events, interviewers escalate to multi-event problems. The phrasing 'at least one' is a classic escalation — and it has a beautiful shortcut.
Calculating P(at least one success) directly means adding up many cases: exactly one, exactly two, exactly three... It's tedious. Instead, use the complement: P(at least one) = 1 - P(none at all). It's almost always faster.
Combinations (nCr) come into play when order doesn't matter — like choosing a committee from a group, or finding the probability that a hand of cards contains exactly two hearts. The formula nCr = n! / (r! × (n-r)!) counts the number of ways to choose r items from n without caring about order.
The key decision tree is: Does order matter? If yes, use permutations. If no, use combinations. For most probability problems involving draws or selections, order doesn't matter — you're choosing a group, not arranging a sequence. When you combine nCr with the core probability formula, you can solve the most complex-looking aptitude problems in four clean steps: count favourable combinations, count total combinations, divide, simplify.
combinatorics_probability.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# === Combinatorics in Probability: At-Least-One and Multi-Event Problems ===import math
defcombinations(n, r):
"""nCr — number of ways to choose r items from n (order doesn't matter)"""
return math.comb(n, r) # Python 3.8+ has this built-in# ---------------------------------------------------------------# PROBLEM 1: "At Least One" — Use the Complement Trick# ---------------------------------------------------------------# Q: A bag has 4 red and 6 blue marbles. Draw 3 without replacement.# What's P(at least one red marble)?
total_marbles = 10
red = 4
blue = 6
draw = 3# Total ways to draw 3 from 10
total_ways = combinations(total_marbles, draw) # 10C3 = 120# P(at least one red) = 1 - P(NO red at all)# 'No red' means all 3 are blue — choose 3 from 6 blue marbles
all_blue_ways = combinations(blue, draw) # 6C3 = 20
p_no_red = all_blue_ways / total_ways
p_at_least_one_red = 1 - p_no_red # the complement shortcutprint("=== PROBLEM 1: At Least One Red ===")
print(f"Total ways to draw 3 from 10 : 10C3 = {total_ways}")
print(f"Ways to draw 3 all-blue : 6C3 = {all_blue_ways}")
print(f"P(no red) = {all_blue_ways}/{total_ways} = {p_no_red:.4f}")
print(f"P(at least one red) = 1 - {p_no_red:.4f} = {p_at_least_one_red:.4f}")
print()
# ---------------------------------------------------------------# PROBLEM 2: Exactly K Successes — Combinations in Numerator# ---------------------------------------------------------------# Q: From a group of 7 men and 5 women, a committee of 4 is formed randomly.# What's P(exactly 2 men and 2 women on the committee)?
total_people = 12# 7 men + 5 women
men = 7
women = 5
committee_size = 4# Total ways to form a committee of 4 from 12 people
total_committees = combinations(total_people, committee_size) # 12C4 = 495# Favourable: choose exactly 2 men from 7 AND exactly 2 women from 5# These are independent choices for the two sub-groups
ways_to_pick_2_men = combinations(men, 2) # 7C2 = 21
ways_to_pick_2_women = combinations(women, 2) # 5C2 = 10
favourable_committees = ways_to_pick_2_men * ways_to_pick_2_women # 210
p_exactly_2_and_2 = favourable_committees / total_committees
print("=== PROBLEM 2: Exactly 2 Men and 2 Women ===")
print(f"Total committee arrangements : 12C4 = {total_committees}")
print(f"Ways to pick 2 men from 7 : 7C2 = {ways_to_pick_2_men}")
print(f"Ways to pick 2 women from 5 : 5C2 = {ways_to_pick_2_women}")
print(f"Favourable arrangements : {ways_to_pick_2_men} × {ways_to_pick_2_women} = {favourable_committees}")
print(f"P(exactly 2 men, 2 women) = {favourable_committees}/{total_committees} = {p_exactly_2_and_2:.4f}")
print()
# ---------------------------------------------------------------# PROBLEM 3: Independent Repeated Trials — Coin Flipped 5 Times# ---------------------------------------------------------------# Q: What's P(getting exactly 3 Heads in 5 fair coin flips)?# This is a Binomial scenario: n=5 trials, k=3 successes, p=0.5
n_flips = 5
k_heads = 3
p_head = 0.5
p_tail = 1 - p_head
# Ways to arrange 3 heads in 5 flips: 5C3
arrangements = combinations(n_flips, k_heads) # = 10
p_exactly_3_heads = arrangements * (p_head ** k_heads) * (p_tail ** (n_flips - k_heads))
print("=== PROBLEM 3: Exactly 3 Heads in 5 Flips ===")
print(f"Arrangements of 3H in 5 flips: 5C3 = {arrangements}")
print(f"P(H)^3 = {p_head}^3 = {p_head**k_heads}")
print(f"P(T)^2 = {p_tail}^2 = {p_tail**(n_flips-k_heads)}")
print(f"P(exactly 3 Heads) = {arrangements} × {p_head**k_heads} × {p_tail**2} = {p_exactly_3_heads:.4f}")
Output
=== PROBLEM 1: At Least One Red ===
Total ways to draw 3 from 10 : 10C3 = 120
Ways to draw 3 all-blue : 6C3 = 20
P(no red) = 20/120 = 0.1667
P(at least one red) = 1 - 0.1667 = 0.8333
=== PROBLEM 2: Exactly 2 Men and 2 Women ===
Total committee arrangements : 12C4 = 495
Ways to pick 2 men from 7 : 7C2 = 21
Ways to pick 2 women from 5 : 5C2 = 10
Favourable arrangements : 21 × 10 = 210
P(exactly 2 men, 2 women) = 210/495 = 0.4242
=== PROBLEM 3: Exactly 3 Heads in 5 Flips ===
Arrangements of 3H in 5 flips: 5C3 = 10
P(H)^3 = 0.5^3 = 0.125
P(T)^2 = 0.5^2 = 0.25
P(exactly 3 Heads) = 10 × 0.125 × 0.25 = 0.3125
Interview Gold: The Complement Trick saves you every time
Whenever you see 'at least one', 'at least two', or 'one or more' in a problem, immediately flip to the complement. P(at least one) = 1 - P(zero). This turns a multi-case addition problem into a single clean calculation. Interviewers watch for this — it signals mathematical maturity.
Production Insight
Using permutations when order doesn't matter overcounts combinations by a factor of r!, skewing probability estimates in A/B test allocation.
Example: Calculating the number of ways to assign users to variants incorrectly inflated the control group size.
Rule: Ask 'does swapping two selected items change the outcome?' If not, use nCr.
Key Takeaway
Combinations (nCr) when order doesn't matter.
Complement trick for 'at least one'.
Exact k successes: binomial formula nCk p^k (1-p)^(n-k).
Conditional Probability and Bayes' Theorem — When New Information Changes Everything
Conditional probability answers the question: "Given that something has already happened, how does that change the odds of something else?" It's written P(B|A) — the probability of event B given that event A has occurred. The multiplication rule for dependent events is P(A and B) = P(A) × P(B|A).
Bayes' Theorem takes this further. It lets you reverse the condition: if you know P(B|A) and the individual probabilities, you can compute P(A|B). The formula is: P(A|B) = [P(B|A) × P(A)] / P(B).
A classic example: A medical test for a rare disease is 99% accurate. If you test positive, what's the probability you have the disease? Most people say 99%. But if the disease affects 1 in 10,000 people, Bayes shows the actual probability is about 1%. The false positives drown out the true positives because the base rate is so low.
In interview problems, Bayes often appears as "there's a bag of red and blue marbles, and you draw one but don't look at it; based on a clue, what's the chance it's red?" The key is to update the sample space with the new information.
bayes_theorem_example.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# === Bayes' Theorem: Medical Test Example ===# A rare disease affects 1 in 10,000 people. The test correctly identifies the disease 99% of the time# (true positive rate = 0.99). The test has a false positive rate of 1%.# If a person tests positive, what is the probability they actually have the disease?# Prior probability of having the disease
p_disease = 1 / 10000# 0.0001# Probability of testing positive given the disease (sensitivity)
p_pos_given_disease = 0.99# Probability of testing positive given no disease (false positive rate)
p_pos_given_no_disease = 0.01# Probability of not having the disease
p_no_disease = 1 - p_disease
# Total probability of testing positive: P(pos) = P(pos|disease)*P(disease) + P(pos|no disease)*P(no disease)
p_pos = (p_pos_given_disease * p_disease) + (p_pos_given_no_disease * p_no_disease)
# Bayes: P(disease|pos) = P(pos|disease)*P(disease) / P(pos)
p_disease_given_pos = (p_pos_given_disease * p_disease) / p_pos
print("=== Bayes' Theorem: Medical Test ===")
print(f"P(disease) = {p_disease:.6f} (1 in 10,000)")
print(f"P(pos|disease) = {p_pos_given_disease}")
print(f"P(pos|no disease) = {p_pos_given_no_disease}")
print(f"\nTotal probability of positive test: P(pos) = {p_pos:.6f}")
print(f"Probability of having the disease given a positive test: P(disease|pos) = {p_disease_given_pos:.4f}")
print(f"\n=> Only about {p_disease_given_pos*100:.1f}% — far lower than the 99% most people guess!")
# Sanity check: use simulation to verifyimport random
random.seed(42)
simulations = 100_000
positive_tests = 0
actual_disease = 0for _ inrange(simulations):
has_disease = random.random() < p_disease
if has_disease:
test_positive = random.random() < p_pos_given_disease
else:
test_positive = random.random() < p_pos_given_no_disease
if test_positive:
positive_tests += 1if has_disease:
actual_disease += 1print(f"\nSimulation: {positive_tests} positive tests out of {simulations}")
print(f"Of those, {actual_disease} actually had the disease: {actual_disease/positive_tests:.4f}")
Output
=== Bayes' Theorem: Medical Test ===
P(disease) = 0.0001 (1 in 10,000)
P(pos|disease) = 0.99
P(pos|no disease) = 0.01
Total probability of positive test: P(pos) = 0.0101
Probability of having the disease given a positive test: P(disease|pos) = 0.0098
=> Only about 1.0% — far lower than the 99% most people guess!
Simulation: 1005 positive tests out of 100000
Of those, 9 actually had the disease: 0.0090
Mental Model: Updating Probabilities Like a Bayesian
Prior: P(A) — your initial belief before seeing evidence.
Likelihood: P(B|A) — how likely the evidence is if your belief is true.
Marginal: P(B) — total probability of the evidence under all possibilities.
Posterior: P(A|B) — updated belief after seeing the evidence.
The formula is symmetric: it works for any two events, not just medical tests.
Production Insight
In production monitoring, Bayes' theorem updates alert thresholds based on prior incident frequency.
Ignoring the prior (base rate) leads to overwhelming false positives when the event is rare.
Rule: Always incorporate the base rate when interpreting conditional probabilities.
Key Takeaway
P(A|B) = P(B|A) * P(A) / P(B)
Bayes updates beliefs with new evidence.
Base rate neglect is the leading cause of overestimating rare event probabilities.
Expected Value and Decision Making Under Uncertainty
Expected value (EV) is the average outcome you'd get if you repeated an experiment many times. It's calculated as the sum of each outcome multiplied by its probability: EV = Σ (value × probability).
For example, a game where you roll a die: if it's 6 you win $10, otherwise you lose $2. The expected value is (1/6 × $10) + (5/6 × -$2) = $1.67 - $1.67 = $0. So the game is fair — no advantage either way.
In interviews, EV problems often appear as "should you play this game?" or "what's the fair price for a ticket?" The key is to list all possible outcomes, their probabilities, and their values, then sum.
Expected value extends to decision trees — when you have choices with probabilistic outcomes, choose the one with the highest EV. But always consider risk: a game with high variance might be avoided even if EV is positive, if losing hurts too much.
expected_value_game.pyPYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# === Expected Value: Decision Making Under Uncertainty ===defexpected_value(outcomes, probabilities):
"""
outcomes: list of numerical values (e.g., winnings)
probabilities: list of probabilities (must sum to 1)
"""
ifabs(sum(probabilities) - 1) > 1e-10:
raiseValueError("Probabilities must sum to 1.")
ev = sum(v * p for v, p inzip(outcomes, probabilities))
return ev
# --- Game 1: Die roll ---# Roll a fair die. If 6: win $10. Otherwise: lose $2.
outcomes = [10, -2]
probabilities = [1/6, 5/6]
ev1 = expected_value(outcomes, probabilities)
print("=== Game 1: Roll a Die ===")
print(f"Outcomes: win $10 (P=1/6), lose $2 (P=5/6)")
print(f"Expected Value = ${ev1:.2f}")
print("(Fair game — expected value is zero)")
print()
# --- Game 2: Raffle ticket ---# 1000 tickets sold at $5 each. Prizes: 1 grand prize of $1000, 5 consolation prizes of $100.# What is the expected value of buying one ticket?
ticket_price = 5
prizes = [(1000, 1), (100, 5)]
total_tickets = 1000# Compute expected payout
payout_ev = sum(prize * count / total_tickets for prize, count in prizes)
# Net expected value: payout minus cost
net_ev = payout_ev - ticket_price
print("=== Game 2: Raffle Ticket ===")
print(f"Ticket price: ${ticket_price}")
print(f"Payout EV: ${payout_ev:.2f}")
print(f"Net EV (including cost): ${net_ev:.2f}")
print("(Negative EV — not a good investment unless you enjoy the cause)")
print()
# --- Game 3: Decision tree with two choices ---# Choice A: Invest $100 in a 60% chance of $200 return, 40% chance of $50 return.# Choice B: Keep the $100 (sure thing).# Which has higher expected value?
choice_a_outcomes = [200, 50] # returns, not net
choice_a_probs = [0.6, 0.4]
ev_a = expected_value(choice_a_outcomes, choice_a_probs) - 100# subtract investment
ev_b = 100 - 100# keep it: net = 0print("=== Game 3: Decision Tree ===")
print(f"Choice A: Invest $100 -> EV = ${ev_a:.2f}")
print(f"Choice B: Keep $100 -> EV = ${ev_b:.2f}")
if ev_a > ev_b:
print("Decision: Invest (higher EV)")
else:
print("Decision: Keep (lower risk, higher EV)")
Output
=== Game 1: Roll a Die ===
Outcomes: win $10 (P=1/6), lose $2 (P=5/6)
Expected Value = $0.00
(Fair game — expected value is zero)
=== Game 2: Raffle Ticket ===
Ticket price: $5
Payout EV: $1.50
Net EV (including cost): -$3.50
(Negative EV — not a good investment unless you enjoy the cause)
=== Game 3: Decision Tree ===
Choice A: Invest $100 -> EV = $20.00
Choice B: Keep $100 -> EV = $0.00
Decision: Invest (higher EV)
Pro Tip: Always separate payout from net profit
When computing expected value, subtract the cost of playing separately. Many candidates miscalculate by forgetting to account for the ticket price or initial investment. Net EV = (sum of prize * probability) - cost.
Production Insight
Expected value calculations drive feature prioritization: multiply impact by probability of success.
A common mistake is to ignore the variance, leading to risky bets that fail in production.
Rule: Use expected value for decisions, but also consider risk (variance) for critical systems.
Key Takeaway
EV = Σ (value × probability)
Use EV to compare probabilistic outcomes.
Don't ignore variance in high-stakes decisions.
● Production incidentPOST-MORTEMseverity: high
The False Assumption of Uniformity in Recommendation Scoring
Symptom
Top recommendations had an abysmal click-through rate (CTR) — far below random selection — despite model training having converged.
Assumption
All content items are equally likely to be interesting to a user, so the probability of a click is uniform across all items.
Root cause
The system used a simple probability calculation that assumed equally likely outcomes, ignoring historical click distributions. Popular items had much higher prior probabilities, but the model treated every item the same, so rare items were recommended as often as popular ones, reducing overall engagement.
Fix
Estimate prior probabilities of engagement from historical data using click counts. Then use Bayes' theorem to update probabilities based on user context. This shifted recommendations toward items users were actually likely to click.
Key lesson
Never assume equal likelihood without justification — always check historical data.
Probability calculations are only as good as the input assumptions; base rates matter.
Bayesian updating turns a naive probability model into a high-performing recommendation system.
Production debug guideIdentify and fix common probability mistakes that skew experiment results3 entries
Symptom · 01
Reported p-value is lower than expected for the observed difference between variants.
→
Fix
Check if independence assumption is violated — e.g., same user seeing both variants due to cross-contamination. Recalculate using proper user-level randomization.
Symptom · 02
Confidence intervals are too narrow, claiming significance when it's not there.
→
Fix
Verify sample size calculation: ensure it accounts for expected variance and that no peeking bias reduced effective sample size.
Symptom · 03
Probability of being best (P(best)) is overestimated in multi-variant tests.
→
Fix
Recalculate using Bayesian approach with a prior that reflects historical experiment results, not just flat prior. Use simulation to correct for multiple comparison bias.
Probability Rules Quick Reference
Scenario
Rule to Use
Formula
Example
Event A OR Event B (mutually exclusive)
Addition Rule — no overlap
P(A) + P(B)
P(roll 1 or roll 2) = 1/6 + 1/6 = 1/3
Event A OR Event B (can overlap)
Addition Rule — subtract overlap
P(A) + P(B) − P(A∩B)
P(King or Heart) = 4/52 + 13/52 − 1/52
Event A AND Event B (independent)
Multiplication Rule — independent
P(A) × P(B)
P(H then H) = 1/2 × 1/2 = 1/4
Event A AND Event B (dependent)
Multiplication Rule — conditional
P(A) × P(B|A)
P(Ace then Ace, no replace) = 4/52 × 3/51
Selecting a group, order doesn't matter
Combinations (nCr)
n! / (r! × (n−r)!)
3 people from 10: 10C3 = 120 ways
At least one success in multiple draws
Complement Method
1 − P(zero successes)
P(at least 1 red) = 1 − P(all blue)
Exactly k successes in n independent trials
Binomial Formula
nCk × p^k × (1−p)^(n−k)
Exactly 3H in 5 flips = 10 × 0.125 × 0.25
Update probability with new evidence
Bayes' Theorem
P(A|B) = P(B|A)×P(A) / P(B)
P(disease|positive) = (0.99×0.0001)/0.0101
Average outcome over many trials
Expected Value
Σ (value × probability)
Raffle EV = $1.50 payout − $5 cost = -$3.50
Key takeaways
1
The core formula
favourable outcomes divided by total equally likely outcomes — is the single idea every probability rule derives from. If you forget a formula, come back to this and count.
2
OR means add (then subtract overlap). AND means multiply (but adjust for dependency if there's no replacement). Confusing these two is the root cause of most wrong answers.
3
The complement trick
P(at least one) = 1 − P(none) — converts the hardest-looking problems into one-step calculations. Reach for it whenever you see 'at least one' or 'one or more'.
4
Always clarify replacement before solving. With replacement → independent events → multiply raw probabilities. Without replacement → dependent events → reduce numerator AND denominator after each draw.
5
Bayes' theorem updates initial beliefs with new evidence; always account for the base rate to avoid false confidence.
Common mistakes to avoid
5 patterns
×
Forgetting to subtract the overlap in OR problems
Symptom
Answer is slightly too high because outcomes that satisfy both events are counted twice.
Fix
Always ask: 'Can both events happen to the same outcome?' If yes, compute P(A) + P(B) - P(A and B).
×
Assuming draws are independent when there's no replacement
Symptom
Probability stays constant across draws (e.g., always dividing by 52 instead of reducing denominator).
Fix
Mentally reduce both numerator (remaining favourable items) and denominator (remaining total) after each draw before multiplying.
×
Using permutations when combinations is correct
Symptom
Answer is exactly a factor of r! too small (because you overcounted arrangements).
Fix
Ask: 'Does swapping two chosen items create a different outcome?' For committees and groups, it doesn't — use nCr.
×
Ignoring the base rate applying Bayes' theorem
Symptom
Overestimating the probability of a rare event after a positive signal (e.g., medical test).
Fix
Always multiply the prior probability by the likelihood; compute marginal probability before dividing.
×
Confusing expected value with guaranteed outcome
Symptom
Expecting a positive EV game to always yield profit in small sample.
Fix
Remember: EV is a long-run average; variance can cause losses in a single trial. Consider risk tolerance.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01SENIOR
A bag has 5 red and 3 green balls. Two balls are drawn without replaceme...
Q02SENIOR
What is the probability of getting at least one six when rolling two fai...
Q03SENIOR
Three people each independently try to solve a problem. Their individual...
Q04SENIOR
You have two urns: one with 2 black and 3 white balls, another with 4 bl...
Q01 of 04SENIOR
A bag has 5 red and 3 green balls. Two balls are drawn without replacement. What is the probability that both balls are red? Walk me through your reasoning step by step.
ANSWER
Step 1: Identify that this is a without-replacement problem, so draws are dependent. Step 2: P(first red) = 5/8 (5 red out of 8 total). Step 3: After drawing a red, there are 4 red left and 7 total balls left. So P(second red | first was red) = 4/7. Step 4: Multiply: P(both red) = (5/8) * (4/7) = 20/56 = 5/14 ≈ 0.3571. The key is to reduce both numerator and denominator after each draw.
Q02 of 04SENIOR
What is the probability of getting at least one six when rolling two fair dice? How would your approach change if I said 'exactly one six'?
ANSWER
Use the complement trick: P(at least one six) = 1 - P(no six). P(no six on a die) = 5/6. Since dice are independent, P(no six on both) = (5/6)(5/6) = 25/36. So P(at least one six) = 1 - 25/36 = 11/36 ≈ 0.3056.
For 'exactly one six', we need two cases: first die six, second not; or first not, second six. Each case has probability (1/6)(5/6) = 5/36. Sum = 10/36 = 5/18 ≈ 0.2778. Note: exactly one six is less likely than at least one because the double-six case is excluded.
Q03 of 04SENIOR
Three people each independently try to solve a problem. Their individual success probabilities are 1/2, 1/3, and 1/4. What's the probability that the problem gets solved? (Tricky follow-up: what if I asked for the probability that exactly one person solves it?)
ANSWER
For 'problem gets solved' = at least one solves. Use complement: P(none solves) = (1-1/2)(1-1/3)(1-1/4) = (1/2)(2/3)(3/4) = 6/24 = 1/4. So P(at least one) = 1 - 1/4 = 3/4.
For 'exactly one solves', we sum three cases:
- Only A solves: (1/2)(2/3)(3/4) = 6/24 = 1/4
- Only B solves: (1/2)(1/3)(3/4) = 3/24 = 1/8
- Only C solves: (1/2)(2/3)(1/4) = 2/24 = 1/12
Total = 1/4 + 1/8 + 1/12 = 6/24 + 3/24 + 2/24 = 11/24 ≈ 0.4583.
This shows that even though the overall probability of solving is high, exactly one person solving is less common.
Q04 of 04SENIOR
You have two urns: one with 2 black and 3 white balls, another with 4 black and 1 white ball. You pick an urn at random (equal probability) and draw a ball. It's white. What is the probability you picked the first urn?
ANSWER
This is a Bayes' theorem problem. Let U1 be first urn, U2 second urn. P(U1) = 1/2, P(U2) = 1/2.
P(white|U1) = 3/5, P(white|U2) = 1/5.
P(white) = P(white|U1)P(U1) + P(white|U2)P(U2) = (3/5)(1/2) + (1/5)(1/2) = (3/10 + 1/10) = 4/10 = 2/5.
Now P(U1|white) = P(white|U1)P(U1) / P(white) = (3/5 1/2) / (2/5) = (3/10) / (2/5) = (3/10)*(5/2) = 15/20 = 3/4 = 0.75.
So there's a 75% chance you picked the first urn given a white ball was drawn.
01
A bag has 5 red and 3 green balls. Two balls are drawn without replacement. What is the probability that both balls are red? Walk me through your reasoning step by step.
SENIOR
02
What is the probability of getting at least one six when rolling two fair dice? How would your approach change if I said 'exactly one six'?
SENIOR
03
Three people each independently try to solve a problem. Their individual success probabilities are 1/2, 1/3, and 1/4. What's the probability that the problem gets solved? (Tricky follow-up: what if I asked for the probability that exactly one person solves it?)
SENIOR
04
You have two urns: one with 2 black and 3 white balls, another with 4 black and 1 white ball. You pick an urn at random (equal probability) and draw a ball. It's white. What is the probability you picked the first urn?
SENIOR
FAQ · 4 QUESTIONS
Frequently Asked Questions
01
What is the difference between mutually exclusive and independent events?
Mutually exclusive means the two events cannot happen at the same time — rolling a 1 and rolling a 2 on a single die. Independent means one event doesn't affect the probability of the other — flipping a coin and rolling a die. Crucially, mutually exclusive events are NOT independent: if A happens, B definitely can't, so knowing A completely changes B's probability.
Was this helpful?
02
When do I use combinations vs permutations in probability problems?
Use combinations (nCr) when selecting a group where order doesn't matter — a committee, a hand of cards, a lottery ticket. Use permutations (nPr) when the arrangement itself matters — a ranked podium, a password, a sequence of draws where first and second positions are different outcomes. Most aptitude probability questions involving selections use combinations.
Was this helpful?
03
How do I know whether to add or multiply probabilities?
Add probabilities when the question involves OR — you want event A or event B to happen. Multiply when the question involves AND — you want event A to happen and then event B to happen. A quick mental test: replace the connecting word with its symbol. 'Or' → + (then subtract overlap if events share outcomes). 'And' → × (then use conditional probability if draws are without replacement).
Was this helpful?
04
How do you calculate the probability of at least one event across multiple independent trials?
Use the complement: P(at least one success) = 1 - P(no success). For independent events with different probabilities, P(no success) = (1-p1) (1-p2) ... * (1-pn). If all probabilities are equal, P(at least one) = 1 - (1-p)^n. This method is always simpler than summing the cases of exactly one, exactly two, etc.