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?

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 together with
. Small symmetric chains have memorable closed forms, such as
for winning a fair gambler’s ruin and
for its expected length. For irregular boards like snakes and ladders, interviewers want the state definition, the system
, 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 for each transient state with
once absorbed. In matrix form this is the fundamental matrix
, whose entries count expected visits.
(3) Long-Run Questions Use The Stationary Equations: Solve with normalization. A finite irreducible and aperiodic chain converges to
from any start, and the mean return time to state
is
.
(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.

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 per bet, the expected remaining bets from bankroll
satisfy
with
. 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
.
Mathematical Formulation:
Where:
is the expected number of steps to absorption from transient state
, and
is the one-step transition probability from
to
; the leading 1 counts the step just taken.
is any absorbing state, where the boundary condition
closes the system.
- With states ordered transient first,
is the transient-to-transient block of
,
the transient-to-absorbing block, and
the identity. The entry
of the fundamental matrix is the expected number of visits to
starting from
, counting the start.
is the vector of expected absorption times,
is the all-ones column, and
is the probability of being absorbed in
when starting from
.
is a row vector (the left eigenvector of
for eigenvalue 1). It is unique when the chain is irreducible, and
converges to rows of
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.
Both values match with
. The fundamental matrix gives the same answer with more detail:
has determinant
, so
and
, and each row sums to 2 expected steps. The probability of reaching state 3 from state 1 is
, which is
. Checking a small case against the closed form like this is the fastest way to catch an off-by-one boundary in an interview.
| Aspect | First-Step Analysis | Fundamental Matrix | Stationary Equations |
|---|---|---|---|
| Question it answers | Expected steps or win probability from a start state | Expected visits, steps, and absorption odds for every start at once | Long-run fraction of time and mean return time |
| Core equation | E_i = 1 + sum_j P_ij E_j, with E = 0 once absorbed | N = (I – Q)^-1, t = N1, B = NR | pi P = pi, entries of pi sum to 1 |
| Closed form expected when | Few states or a symmetric walk (gambler’s ruin, waiting for HH) | Two or three transient states by hand | Two states, or reversible chains via detailed balance |
| Classic instance | Fair coin from 3 dollars to 10: 21 bets on average | Snakes and ladders: 100 unknowns, solved in code | Random knight on a chessboard: corner return time 168 |
| Trap | Expectation can be infinite on an unbounded walk | States must be ordered transient first, absorbing last | A periodic chain never converges in distribution |
Leave a Reply