ML0103 Markov Chain Interview Puzzles

How do you solve Markov chain and random walk interview questions: expected steps to absorption (gambler’s ruin), stationary distributions, and when do interviewers expect you to just set up the equations?

Two whiteboard sketches. Left: a number line of states 0, 1, 2, 3, 4, 5, N where 0 is marked ruin and N is marked goal as square absorbing states, a walker starts at state k equal to 2, and curved arrows show a step right with probability p and a step left with probability q equal to 1 minus p, with the questions how many bets until the walk hits 0 or N and how likely it is to reach N first. Right: a two-state weather chain with Sunny and Rainy circles, self-loops 0.9 and 0.5, Sunny to Rainy 0.1 and Rainy to Sunny 0.5, with the questions what fraction of days are rainy in the long run and whether the answer depends on today's weather

The Problem: Two chains of the kind interviewers sketch. The left one is a hitting question on a walk between two absorbing walls, and the right one is a long-run question on a chain that never stops moving.

Answer

Almost every Markov chain interview question reduces to one of two linear systems, and the real skill is recognizing which one the story is asking for. If the question asks how long until something happens, or how likely it is to happen first, use first-step analysis. Conditioning on the first move gives one linear equation per transient state, with boundary value zero at every absorbing state. If the question asks about the long run, solve the stationary equations \pi P = \pi together with \sum_i \pi_i = 1. Small symmetric chains have memorable closed forms, such as k/N for winning a fair gambler’s ruin and k(N-k) for its expected length. For irregular boards like snakes and ladders, interviewers want the state definition, the system t = (I-Q)^{-1}\mathbf{1}, and a solver call, not hand algebra.

(1) Define The State First: The state must carry exactly enough memory that the next move depends only on it. For expected flips until two heads in a row, the state is the current run of heads (0, 1, or done), and first-step analysis gives 6 flips.
(2) Absorption Questions Use First-Step Analysis: Write E_i = 1 + \sum_j P_{ij}E_j for each transient state with E = 0 once absorbed. In matrix form this is the fundamental matrix N = (I-Q)^{-1}, whose entries count expected visits.
(3) Long-Run Questions Use The Stationary Equations: Solve \pi P = \pi with normalization. A finite irreducible and aperiodic chain converges to \pi from any start, and the mean return time to state i is 1/\pi_i.
(4) Know When To Stop At Equations: Closed forms are expected for small or symmetric chains such as gambler’s ruin, two-state chains, and coin-pattern waiting times. For a 100-square board, a correct system plus an invertibility argument is the full answer.

Flowchart: define the state as the minimum memory that makes the next move depend on it, then decide what is asked. If the question is about reaching a target, the absorbing-chain lane leads to first-step analysis E_i equals 1 plus the sum of P_ij E_j or t equals the inverse of I minus Q times the ones vector, ending in a closed form when the chain is symmetric or small and a numeric solve otherwise. If the question is about the long run, the ergodic-chain lane leads to the stationary equations pi P equals pi with entries summing to 1 or detailed balance, ending in checks for irreducibility and aperiodicity and the return time 1 over pi_i. Notes at the bottom list sanity checks and the advice to state the system and solve it in code for large boards

Figure 1: The solving recipe. The state definition decides whether the chain is Markov at all, and the question type selects between the absorbing-chain system and the stationary system before any algebra starts.

The recipe in Figure 1 works because a Markov chain has no memory beyond its current state, so conditioning on one step turns a question about an unbounded random path into a finite set of equations. For gambler’s ruin with win probability p per bet, the expected remaining bets from bankroll k satisfy E_k = 1 + pE_{k+1} + qE_{k-1} with E_0 = E_N = 0. Stacking these equations over all transient states gives exactly the matrix system below. Unlike a hidden Markov model, every state here is observed, so there is no inference over latent states, only linear algebra on P.

Mathematical Formulation:
E_i = 1 + \sum_{j} P_{ij} E_j
E_a = 0
N = (I - Q)^{-1}
t = N\mathbf{1}
B = NR
\pi P = \pi
\sum_{i} \pi_i = 1

Where:

  • E_i is the expected number of steps to absorption from transient state i, and P_{ij} is the one-step transition probability from i to j; the leading 1 counts the step just taken.
  • a is any absorbing state, where the boundary condition E_a = 0 closes the system.
  • With states ordered transient first, Q is the transient-to-transient block of P, R the transient-to-absorbing block, and I the identity. The entry N_{ij} of the fundamental matrix is the expected number of visits to j starting from i, counting the start.
  • t is the vector of expected absorption times, \mathbf{1} is the all-ones column, and B_{ia} is the probability of being absorbed in a when starting from i.
  • \pi is a row vector (the left eigenvector of P for eigenvalue 1). It is unique when the chain is irreducible, and P^n converges to rows of \pi only if the chain is also aperiodic.

Worked Example:
A drunkard stands on step 1 of a walkway with states 0 to 3, moves left or right with probability 1/2, and stops at either end. The transient states are 1 and 2, so first-step analysis gives two equations.

t_1 = 1 + \tfrac{1}{2}t_2
t_2 = 1 + \tfrac{1}{2}t_1
t_1 = t_2 = 2

Both values match k(N-k) with N = 3. The fundamental matrix gives the same answer with more detail: I - Q has determinant 3/4, so N_{11} = N_{22} = 4/3 and N_{12} = N_{21} = 2/3, and each row sums to 2 expected steps. The probability of reaching state 3 from state 1 is N_{12} \cdot \tfrac{1}{2} = 1/3, which is k/N. Checking a small case against the closed form like this is the fastest way to catch an off-by-one boundary in an interview.

AspectFirst-Step AnalysisFundamental MatrixStationary Equations
Question it answersExpected steps or win probability from a start stateExpected visits, steps, and absorption odds for every start at onceLong-run fraction of time and mean return time
Core equationE_i = 1 + sum_j P_ij E_j, with E = 0 once absorbedN = (I – Q)^-1, t = N1, B = NRpi P = pi, entries of pi sum to 1
Closed form expected whenFew states or a symmetric walk (gambler’s ruin, waiting for HH)Two or three transient states by handTwo states, or reversible chains via detailed balance
Classic instanceFair coin from 3 dollars to 10: 21 bets on averageSnakes and ladders: 100 unknowns, solved in codeRandom knight on a chessboard: corner return time 168
TrapExpectation can be infinite on an unbounded walkStates must be ordered transient first, absorbing lastA periodic chain never converges in distribution

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *