Skip to content

Practice Questions

New questions generated in the style of the problems in interview.md. Each question has a collapsible answer — click to reveal. Attempt the problem before opening the solution.

2 Brainteasers

2.2 Logic and Reasoning

Bridge Crossing (4 people, 1 torch)

Four people — A (8 min), B (4 min), C (2 min), D (1 min) — need to cross a bridge at night. The bridge holds at most two people at a time, and they have only one torch, which must be carried on every crossing. What is the minimum total time to get everyone across?

Solution

Send the two slowest together, and use the fastest to shuttle the torch back.

  • C + D cross: 2 min
  • D returns: 1 min
  • A + B cross: 8 min
  • C returns: 2 min
  • C + D cross: 2 min

Total = 2 + 1 + 8 + 2 + 2 = 15 min.

The key insight is that the two slowest (A, B) should cross together so their 8 min is paid only once, and the fastest (D) does the torch shuttling.

Birthday Problem (variant)

A's birthday is one of the following dates. A tells only the month to You, and only the day to C.

  • May 15, May 16, May 19
  • Jun 17, Jun 18
  • Jul 14, Jul 16
  • Aug 14, Aug 15, Aug 17

You say: "I don't know A's birthday, and I know C doesn't know it either." C says: "At first I didn't know, but now I do." You say: "Now I know it too."

What is A's birthday?

Solution

This is the classic Cheryl's Birthday problem.

  1. You (month) says C doesn't know. This rules out months that contain a unique day — a day that appears only once across all dates. Jun 18 and May 19 are unique days, so the month cannot be May or June.
  2. C (day) now knows. Among the remaining months (Jul, Aug), the day must be unique. Jul 14 and Aug 14 share the day 14, so it cannot be the 14th. The remaining candidates are Jul 16, Aug 15, Aug 17.
  3. You (month) now knows. If the month were August, there would still be two options (15, 17), so it must be a month with a single remaining candidate: July.

Answer: July 16.

Card game (red/black, EV)

A casino deals a standard 52-card deck. You turn over two cards at a time. If both are black, they go to the dealer's pile; if both are red, they go to your pile; if one of each, they are discarded. If you end with more cards than the dealer, you win $100; otherwise you get nothing. What is the fair price to pay for this game?

Solution

By symmetry, the number of red pairs you collect equals the number of black pairs the dealer collects in expectation — the game is fair in terms of card counts. However, ties count as a loss for you.

The probability of a strict win is less than 1/2 (there is a positive probability of a tie), so the expected value is negative. You should not pay anything to play — the fair price is $0 (or you simply decline).

The house edge comes entirely from the tie rule, not from the card mechanics.

Burning ropes (measure 45 min)

You have two ropes, each burning in exactly 1 hour, but at non-uniform rates (you cannot rely on half the rope burning in 30 min). Using only these two ropes and a lighter, how do you measure exactly 45 minutes?

Solution
  1. Light rope 1 at both ends and rope 2 at one end simultaneously.
  2. Rope 1 burns completely in 30 min (both ends). At that moment, rope 2 has 30 min of burn left.
  3. Immediately light the other end of rope 2. It now burns from both ends and finishes in 15 min.

Total = 30 + 15 = 45 min.

Defective ball (9 balls, 2 weighings)

You have 9 identical-looking balls. Exactly one is heavier than the rest (you know it is heavier, not lighter). Using a balance scale, find the heavy ball in exactly 2 weighings.

Solution

Split the 9 balls into three groups of 3: A, B, C.

  1. Weigh A vs B.
  2. If A > B: the heavy ball is in A.
  3. If A < B: the heavy ball is in B.
  4. If A = B: the heavy ball is in C.
  5. From the identified group of 3, weigh any two balls against each other.
  6. If one is heavier, that's the heavy ball.
  7. If they balance, the third ball is heavy.

Each weighing has 3 outcomes, so \(3^2 = 9\) outcomes cover exactly 9 cases.

Trailing zeros (500!)

How many trailing zeros are in \(500!\)?

Solution

Trailing zeros come from factors of 10 = 2 × 5. Since 2s are far more abundant than 5s, count the number of factors of 5.

  • Multiples of 5: \(\lfloor 500/5 \rfloor = 100\)
  • Multiples of 25: \(\lfloor 500/25 \rfloor = 20\)
  • Multiples of 125: \(\lfloor 500/125 \rfloor = 4\)
  • Multiples of 625: \(\lfloor 500/625 \rfloor = 0\)

Total = 100 + 20 + 4 = 124 trailing zeros.

25 horses (find top 3)

There are 25 horses, each running at a distinct constant speed. A race can hold at most 5 horses. What is the minimum number of races needed to identify the 3 fastest horses?

Solution

7 races.

  1. 5 races: split into 5 groups of 5, race each group. Record the order within each group.
  2. 1 race: race the 5 group winners. This gives an overall ranking of the groups.

Let the winner of the winners' race be group A, second be group B, third be group C. Eliminate: - Groups D and E entirely (their winners are already slower than 3 horses). - In group C, only its winner can still be in the top 3 (its 2nd and 3rd are slower than its winner, which is already 3rd overall). - In group B, only its winner and 2nd can still qualify. - In group A, its winner is already #1; its 2nd and 3rd can still qualify.

Candidates: A2, A3, B1, B2, C1 (5 horses). 3. 1 race: race these 5. The top 2, combined with A1, give the 3 fastest.

Total = 5 + 1 + 1 = 7 races.

Infinite sequences (nested root)

If \(\sqrt{2 + \sqrt{2 + \sqrt{2 + \cdots}}} = x\), what is \(x\)?

Solution

Let the infinite expression equal \(x\). Then the expression inside the first square root is also \(x\):

\[x = \sqrt{2 + x}\]

Squaring both sides: \(x^2 = 2 + x \Rightarrow x^2 - x - 2 = 0 \Rightarrow (x-2)(x+1) = 0\).

Since \(x > 0\), we take \(x = 2\).

Answer: \(x = 2\).

Escalator (relative motion)

Two people step onto a moving escalator at the same time. Alice climbs 3 steps in the time Bob climbs 2 steps. Alice steps off the top having counted 30 steps; Bob steps off having counted 20 steps. How many steps are visible on the escalator at any instant?

Solution

Let \(D\) be the total number of visible steps, and \(x\) the number of steps the escalator carries a stationary rider.

Alice's speed is \(3/2\) times Bob's. Let Bob's climbing rate be \(v\), Alice's be \(1.5v\).

  • Alice: \(D = 30 + x \cdot \frac{t_A}{t_{total}}\)... more directly, the escalator moves the same number of steps \(x\) for both, but in different times.

Time ratio: Alice counts 30 steps at rate \(1.5v\), so \(t_A = 30/(1.5v) = 20/v\). Bob counts 20 steps at rate \(v\), so \(t_B = 20/v\). They take the same time!

So the escalator contributes the same \(x\) steps to both: - \(D = 30 + x\) - \(D = 20 + x\)

These are inconsistent unless... let's redo. The escalator moves \(x\) steps in the time it takes to climb. Since both take the same time, \(x\) is the same.

\(D = 30 + x\) and \(D = 20 + x\) cannot both hold. The resolution: the escalator's contribution differs because the climb times differ. Let \(k\) be the ratio of Bob's time to Alice's time.

Alice: \(D = 30 + x_A\), Bob: \(D = 20 + x_B\), where \(x_B = k \cdot x_A\).

Alice time \(t_A = 30/(1.5v) = 20/v\). Bob time \(t_B = 20/v\). So \(k = 1\) — same time, same escalator contribution \(x\).

\(D = 30 + x = 20 + x\) is impossible, meaning the given numbers are inconsistent. This is a trick: with these speeds and counts, no consistent \(D\) exists. The intended setup (as in the original) uses different climb times. With Alice at 3 steps per Bob's 2, and counts 30 vs 20, both take equal time, so the problem as stated has no solution.

(For the original-style numbers — 25 and 20 with a 3:2 speed ratio — the answer is 50.)

2.4 Application of Symmetry

Coin piles (equal heads)

You are blindfolded with 100 coins on the floor: 90 tails-up and 10 heads-up. You may flip any coins but cannot see or feel which side is up. How do you split the coins into two piles so that both piles have the same number of heads?

Solution

Take any 10 coins into a separate pile, and flip all 10 of them.

Let the 10-coin pile contain \(m\) heads (and \(10 - m\) tails). After flipping, it contains \(10 - m\) heads. The remaining 90 coins contain \(10 - m\) heads (the rest of the original 10 heads). Both piles now have exactly \(10 - m\) heads.

Answer: separate 10 coins and flip them.

Mislabelled bags (3 bags)

Three bags are labelled "Apples", "Oranges", and "Mixed". All three labels are wrong. You may reach into one bag and draw a single fruit. How do you determine the contents of all three bags?

Solution

Draw one fruit from the bag labelled "Mixed".

Since all labels are wrong, the "Mixed" bag is actually all apples or all oranges. - If you draw an apple, the "Mixed" bag is the Apples bag. - If you draw an orange, the "Mixed" bag is the Oranges bag.

Now the bag labelled "Apples" cannot be apples (label wrong) and cannot be the bag you just identified, so it must be the Mixed bag. The remaining bag is the last fruit type.

One draw from the "Mixed" bag is sufficient.

Wise men (glass counter)

A sultan has 50 wise men and a glass that starts upside down. Each minute one random wise man is called and may flip the glass or do nothing. When a wise man correctly states that everyone has been called at least once, all go free. They may agree on a strategy beforehand. Design one.

Solution

Designate one counter (say man #1). The other 49 men each flip the glass from upside-down to upright exactly once — the first time they see it upside down — and otherwise do nothing.

The counter, whenever called, flips the glass from upright to upside down (resetting it) and increments a mental count. When the counter has seen the glass upright 49 times, all 49 other men have been called at least once, so the counter announces everyone has been called.

The counter must also handle the initial state: if the glass starts upside down, the counter's first flip-up doesn't count toward the 49.

2.6 Pigeonhole Principle

Matching socks (guarantee a pair)

A drawer has 3 red socks, 4 green socks, and 5 blue socks. What is the minimum number of socks you must grab (without looking) to guarantee a matching pair?

Solution

There are 3 colours, so with 3 socks you could have one of each colour. The 4th sock must match one of them.

Answer: 4 socks (3 pigeonholes, so \(3 + 1\)).

Ants on a square (guarantee 3 in a region)

There are 26 ants on a square of side length 1. Show that you can always place a circle of radius \(1/5\) that covers at least 2 ants.

Solution

Split the square into a \(5 \times 5\) grid of 25 smaller squares, each of side \(1/5\). A circle of radius \(1/5\) can cover one such small square (its circumradius is \(\frac{1/5}{\sqrt2} < 1/5\)).

By the pigeonhole principle, 26 ants in 25 squares means at least one square contains 2 ants. Place the circle over that square.

Answer: guaranteed by pigeonhole.

2.X Others

Jug problem (measure 4L with 5L and 3L)

You have an unlimited water supply, a 5L jug, and a 3L jug (no markings). Measure exactly 4L.

Solution
  1. Fill the 5L jug.
  2. Pour from the 5L jug into the 3L jug until the 3L jug is full. Now the 5L jug has 2L.
  3. Empty the 3L jug.
  4. Pour the 2L from the 5L jug into the 3L jug. The 3L jug now has 2L, with 1L of space.
  5. Fill the 5L jug again.
  6. Pour from the 5L jug into the 3L jug until the 3L jug is full (adds 1L). The 5L jug now has 4L.

Answer: 4L in the 5L jug.

4 Probability

4.1 Basic

Coin Toss Game (A has n+2 coins)

Gambler A has \(n+2\) fair coins; gambler B has \(n\) fair coins. Both flip all their coins. What is the probability that A gets more heads than B?

Solution

Let A's extra two coins be considered separately. First compare A's first \(n\) coins against B's \(n\) coins.

By symmetry, the probability A's \(n\) coins beat B's \(n\) coins equals the probability they lose, and there is a tie with probability \(p_{tie}\).

  • If A's \(n\) coins already beat B: A wins regardless of the 2 extra coins.
  • If A's \(n\) coins tie B: A wins if the 2 extra coins produce at least 1 head, i.e. with probability \(3/4\).
  • If A's \(n\) coins lose to B: A can still win if the 2 extra coins produce 2 heads and the deficit is exactly 1. This is more involved.

A cleaner approach: pair each of A's \(n\) coins with one of B's. For the \(n\) pairs, A wins a pair with prob \(1/4\), loses with prob \(1/4\), ties with prob \(1/2\). A's two extra coins add 0, 1, or 2 heads.

The full computation is tedious; the elegant result is that with \(n+2\) vs \(n\) coins, the probability A has strictly more heads is \(1/2\) (the extra two coins give A a strict advantage only through ties, and the symmetry argument generalises: \(P(\text{A more}) = P(\text{B more})\), and ties are broken in A's favour with probability \(3/4\) of the tie mass, but the net is still \(1/2\)).

Answer: \(1/2\).

Card Game (higher card wins)

You and the dealer each draw one card from a shuffled 52-card deck (without replacement). You win if your card is strictly higher. What is your probability of winning?

Solution

Let \(X\) = you win, \(Y\) = tie, \(Z\) = dealer wins. By symmetry \(P(X) = P(Z)\).

The probability of a tie: after you draw a card, there are 3 cards of the same value among the remaining 51, so \(P(Y) = 3/51 = 1/17\).

Since \(P(X) + P(Y) + P(Z) = 1\) and \(P(X) = P(Z)\):

\[P(X) = \frac{1 - P(Y)}{2} = \frac{1 - 1/17}{2} = \frac{8}{17}\]

Answer: \(8/17\).

Drunk passenger (100 seats)

100 passengers board a plane in order. Passenger 1 (drunk) picks a random seat. Every other passenger sits in their own seat if free, otherwise picks a random free seat. What is the probability passenger 100 gets their own seat?

Solution

Consider only seats #1 and #100. The process is decided by which of these two seats gets taken first by a "displaced" passenger.

Whenever a passenger is forced to choose randomly, they are equally likely to pick seat #1 or seat #100. If seat #100 is taken first, passenger 100 loses; if seat #1 is taken first, passenger 100 wins (because then everyone else can sit properly and seat #100 remains).

By symmetry, the two outcomes are equally likely.

Answer: \(1/2\).

N points on a circle (all in a semicircle)

Given \(N\) points placed uniformly at random on a circle, what is the probability they all lie within some semicircle?

Solution

Fix one point. For each of the other \(N-1\) points, the probability it lies within the semicircle starting at the fixed point is \(1/2\). The events are disjoint for the \(N\) choices of the "starting" point, so:

\[P = N \cdot \left(\frac{1}{2}\right)^{N-1} = \frac{N}{2^{N-1}}\]

Answer: \(\dfrac{N}{2^{N-1}}\).

Quarterfinal (A meets B in semi)

8 players are drawn into a single-elimination quarterfinal bracket. What is the probability that two specific players, A and B, meet in the semi-final (i.e., they are in different quarters and both win their quarterfinal)?

Solution
  • Probability A and B are in different quarters: A is placed in one quarter; B must be in one of the other 3 quarters out of 7 remaining spots: \(3/7\).
  • Probability A wins their quarterfinal: \(1/2\).
  • Probability B wins their quarterfinal: \(1/2\).
\[P = \frac{3}{7} \cdot \frac{1}{2} \cdot \frac{1}{2} = \frac{3}{28}\]

Answer: \(3/28\).

Consecutive Winning (first to 3)

A and B play games; A wins each with probability \(2/3\). The series ends when one player wins 3 games. What is the probability A wins the series?

Solution

Let \(P_i\) be the probability A wins given A is \(i\) games ahead (so \(P_{-3}=0\), \(P_3=1\)).

For \(i \in \{-2,-1,0,1,2\}\): $\(P_i = \frac{2}{3}P_{i+1} + \frac{1}{3}P_{i-1}\)$

Solve the recurrence. Writing the equations: - \(P_2 = \frac{2}{3}(1) + \frac{1}{3}P_1 = \frac{2}{3} + \frac{1}{3}P_1\) - \(P_1 = \frac{2}{3}P_2 + \frac{1}{3}P_0\) - \(P_0 = \frac{2}{3}P_1 + \frac{1}{3}P_{-1}\) - \(P_{-1} = \frac{2}{3}P_0 + \frac{1}{3}P_{-2}\) - \(P_{-2} = \frac{2}{3}P_{-1} + \frac{1}{3}(0) = \frac{2}{3}P_{-1}\)

Solving this system gives \(P_0 = \frac{16}{27} \approx 0.593\).

Answer: \(16/27\).

Two Kings (both cards are Kings)

You are dealt two cards from a shuffled 52-card deck. What is the probability both are Kings?

Solution
\[P = \frac{4}{52} \cdot \frac{3}{51} = \frac{12}{2652} = \frac{1}{221}\]

Answer: \(1/221\).

Dice re-roll (expected payoff)

You roll a fair die. You may keep the value or re-roll once (you must keep the second roll). What is your expected payoff under optimal play?

Solution

Optimal strategy: keep the first roll if it is 4, 5, or 6; re-roll if it is 1, 2, or 3.

  • Probability of rolling 4, 5, or 6: \(1/2\). Expected value if you keep: \((4+5+6)/3 = 5\).
  • Probability of re-rolling: \(1/2\). Expected value of the second roll: \(3.5\).
\[E = \frac{1}{2} \cdot 5 + \frac{1}{2} \cdot 3.5 = 4.25\]

Answer: \(4.25\).

4.2 Combinatorial Analysis

Poker Hands (straight flush vs four of a kind)

From a 5-card hand drawn from a 52-card deck, what is the probability of a straight flush (5 cards of the same suit in consecutive values, including the royal flush)?

Solution

Total hands: \(\binom{52}{5} = 2{,}598{,}960\).

A straight flush: choose the suit (4 ways) and the starting value of the run. Runs can start at A, 2, ..., 10 (10 possible runs, where A can be high or low). So \(4 \times 10 = 40\) straight flushes.

\[P = \frac{40}{2{,}598{,}960} \approx 1.54 \times 10^{-5}\]

Answer: \(40 / \binom{52}{5} \approx 1.54 \times 10^{-5}\).

Hopping Rabbit (n stairs, 1 or 2 steps)

A rabbit can hop 1 or 2 stairs at a time. How many ways to climb \(n\) stairs?

Solution

Let \(f(n)\) be the number of ways. The last hop is either 1 stair (from \(n-1\)) or 2 stairs (from \(n-2\)):

\[f(n) = f(n-1) + f(n-2), \quad f(1)=1, \ f(2)=2\]

This is the Fibonacci sequence. For example, \(f(10) = 89\).

Answer: Fibonacci numbers.

Screwy pirates (locks and keys)

7 pirates agree that any 4 of them together can open the safe, but no 3 can. What is the minimum number of locks, and how many keys does each pirate carry?

Solution

For every group of 3 pirates (who must NOT be able to open the safe), there must be a lock that none of them has a key to, but that every one of the other 4 pirates does have a key to.

Number of 3-pirate groups: \(\binom{7}{3} = 35\) locks.

Each lock is opened by the 4 pirates not in its associated group, so each lock has 4 keys. Total keys = \(35 \times 4 = 140\). Each pirate carries \(140/7 = 20\) keys.

Answer: 35 locks, 20 keys per pirate.

Seating (no two women adjacent)

5 men and 3 women sit around a circular table. How many ways can they be seated so that no two women sit together?

Solution
  1. Arrange the 5 men around the circle: \((5-1)! = 24\) ways.
  2. There are 5 gaps between the men. Choose 3 gaps for the women: \(\binom{5}{3} = 10\).
  3. Arrange the 3 women in those gaps: \(3! = 6\).

Total = \(24 \times 10 \times 6 = 1440\).

Answer: 1440 ways.

4.3 Conditional Probability and Bayes' formula

Boys and girls (at least one boy)

A family has two children. Given that at least one is a boy, what is the probability both are boys?

Solution

Sample space (equally likely): BB, BG, GB, GG. Given at least one boy, we restrict to {BB, BG, GB}. Only BB has two boys.

\[P(\text{BB} \mid \text{at least one boy}) = \frac{1}{3}\]

Answer: \(1/3\).

Unfair coin (Bayes)

You have 1000 coins: 1 is double-headed, 999 are fair. You pick a coin at random and toss it 10 times, getting 10 heads. What is the probability you picked the double-headed coin?

Solution

Let \(A\) = double-headed coin, \(B\) = 10 heads in a row.

  • \(P(A) = 1/1000\)
  • \(P(B \mid A) = 1\)
  • \(P(B \mid A^c) = (1/2)^{10} = 1/1024\)

By Bayes: $\(P(A \mid B) = \frac{P(B \mid A)P(A)}{P(B \mid A)P(A) + P(B \mid A^c)P(A^c)} = \frac{1 \cdot \frac{1}{1000}}{1 \cdot \frac{1}{1000} + \frac{1}{1024} \cdot \frac{999}{1000}}\)$

\[= \frac{1}{1 + \frac{999}{1024}} = \frac{1024}{2023} \approx 0.506\]

Answer: \(\approx 0.506\).

Russian roulette (spin after each trigger)

A 6-chamber revolver has 1 bullet. Two players take turns pulling the trigger, spinning the barrel after every pull. If you can choose, do you go first or second, and what is your probability of losing?

Solution

Let \(p\) = probability the first player loses. The first player loses if the gun fires on their first pull (prob \(1/6\)), or if it doesn't (prob \(5/6\)) and then the roles reverse — the first player now faces the same situation as the second player, whose loss probability is \(1 - p\).

\[p = \frac{1}{6} + \frac{5}{6}(1 - p) \Rightarrow p = \frac{6}{11}\]

The first player loses with probability \(6/11 > 1/2\), so you should go second, with loss probability \(5/11\).

Answer: go second; loss probability \(5/11\).

Aces (each player gets an ace)

52 cards are dealt evenly to 4 players (13 each). What is the probability each player gets exactly one ace?

Solution

Deal the aces first. The first ace can go to any player; the second ace must go to a different player (39 of the remaining 51 positions); the third to a different player (26 of 50); the fourth to the last player (13 of 49).

\[P = 1 \times \frac{39}{51} \times \frac{26}{50} \times \frac{13}{49}\]
\[= \frac{39 \cdot 26 \cdot 13}{51 \cdot 50 \cdot 49} \approx 0.105\]

Answer: \(\approx 0.105\).