The oldest problem in the book, set up cleanly
A gambler starts with a dollars. Each round she bets one dollar on a fair coin: heads she gains a dollar, tails she loses one. She plays until her fortune hits 0 (ruin) or climbs to N (she has cleaned out the table and walks away rich). The two questions everyone asks are: what is the probability she goes broke rather than reaching N, and how long does the game last on average? This is the classic gambler's ruin problem, and it is the perfect first target for the machinery you have been building in this rung.
Let X(n) be her fortune after n rounds, with X(0) = a. Then X(n) is exactly the simple random walk you already know — start at a, take steps of +1 or -1 each with probability 1/2, but with two walls: an absorbing barrier at 0 and another at N. The moment the walk touches a wall, the game stops. Everything below is just a careful reading of that walk through the lens of fair games.
First martingale: the fortune itself
Here is the keystone observation. Because the coin is fair, the fortune X(n) is itself a martingale with respect to the natural filtration generated by the rounds played so far. Given everything up to round n, the next step is +1 or -1 with equal odds, so the conditional expectation of X(n+1) is X(n) + (1/2)(+1) + (1/2)(-1) = X(n). A fair game does not drift up or down on average — that is the entire content of being a martingale, and the previous guides taught you to recognize it on sight.
Let T be the first round at which the walk hits 0 or N. From guide 3 you know T is a stopping time: to decide whether to stop at round n you only need the fortune up to round n, never a peek at the future. Now we want to say E[X(T)] = X(0) = a — that the average stopped fortune equals the starting fortune. That is the promise of the optional stopping theorem, but only when its conditions hold, so we must check them rather than assume them.
The cleanest version of optional stopping that applies here asks for a bounded martingale up to time T, and we have it: between the walls the fortune stays inside [0, N], so |X(n and T)| <= N for all n. (We also need T to be finite with probability 1 — true here, because from any interior point the chance of N straight steps in one direction before stopping is positive, so the game cannot dodge both walls forever.) With a bounded martingale and an almost-surely finite stopping time, optional stopping is rigorously valid: E[X(T)] = a, full stop.
Reading off the ruin probability
When the game stops, the fortune is exactly one of the two walls: X(T) is either 0 or N, nothing in between. So X(T) is a tiny two-valued random variable, and its expectation is easy to write down. Let p be the probability the gambler reaches N (the happy ending). Then with probability 1 - p she is ruined at 0. By linearity of expectation, E[X(T)] = p * N + (1 - p) * 0 = p * N.
Now set the two expressions for E[X(T)] equal. Optional stopping gave us E[X(T)] = a; the two-point calculation gave us E[X(T)] = p * N. Therefore p * N = a, and the probability of the happy ending is simply p = a / N. The probability of ruin is the complement, 1 - a / N = (N - a) / N. One martingale, one theorem, one line of algebra — and the famous answer falls out.
Fair game, start at a, walls at 0 and N, T = first hitting time.
Martingale: X(n) is a martingale -> E[X(T)] = X(0) = a
Stopped value: X(T) in {0, N} -> E[X(T)] = p*N + (1-p)*0
Set equal: p * N = a
P(reach N) = p = a / N
P(ruin at 0)= 1 - p = (N - a) / N
Example a = 30, N = 100: P(reach 100) = 0.30, P(ruin) = 0.70Sanity-check the formula against intuition. Start halfway, a = N/2, and you get p = 1/2 — perfectly symmetric, exactly right for a fair coin. Start with a = 30 against a table worth N = 100 and your chance of breaking the bank is only 0.30: even in a perfectly fair game, the player with less money is far likelier to be ruined, simply because she has less room to absorb a losing streak before hitting the floor. The fairness is in the average step, not in the survival odds.
Second martingale: how long does it last?
The ruin probability used the martingale X(n). To find the expected duration E[T] we need a second, slightly cleverer martingale. Consider M(n) = X(n)^2 - n. Is it a martingale? Each step changes the fortune by +1 or -1, so the squared fortune changes by (X(n) +- 1)^2 - X(n)^2 = +- 2 X(n) + 1; the +- 2 X(n) part averages to 0 because the step is symmetric, leaving an average increase of exactly +1 per round in X(n)^2. Subtracting n cancels that built-in drift, so E[M(n+1) given the past] = M(n). The corrected process M(n) = X(n)^2 - n is a genuine martingale.
Apply optional stopping to M. The technical care is the same in spirit: X(n and T)^2 is bounded by N^2, and although the -n term is not bounded, T has finite expectation here (a fact you can take on trust, or prove with the same wall-hitting argument), which is enough to justify E[M(T)] = M(0). That gives E[X(T)^2 - T] = X(0)^2 - 0, i.e. E[X(T)^2] - E[T] = a^2. The minus-T inside the martingale is precisely what smuggles E[T] into an equation we can solve.
Now finish by reusing what we already know. X(T) is 0 or N, so X(T)^2 is 0 or N^2, and E[X(T)^2] = p * N^2 + (1 - p) * 0 = p * N^2 = (a/N) * N^2 = a * N, using p = a/N from before. Substitute: a * N - E[T] = a^2, which rearranges to E[T] = a * N - a^2 = a (N - a). The expected number of rounds is simply a times (N - a). Plug in a = 30, N = 100: the game lasts on average 30 * 70 = 2100 rounds. A 'small' fair bet can take a very long time to resolve.
Where this can go wrong — and the biased version
The slickness can fool you into skipping the conditions, and optional stopping has real teeth. Drop one wall — let the gambler play a fair coin with no upper target, just down to 0 — and the answers change completely. The fortune is still a martingale, but now it is not bounded above, and applying optional stopping blindly would 'prove' E[X(T)] = a even though X(T) = 0 always (she is ruined with probability 1 in the one-wall fair game), which would absurdly say a = 0. The naive equation breaks because the unbounded one-sided walk violates the integrability conditions; the missing ingredient is exactly the uniform integrability (or a boundedness/finite-stopping condition) that the two-wall version supplied for free.
The honest moral: optional stopping is not a license to swap E[X(T)] for E[X(0)] whenever you please. It needs a genuine reason — a bounded stopped process, a uniformly integrable family, or a bounded stopping time — and the two-wall ruin problem is so clean precisely because the walls hand you that boundedness. Always name your reason before you write E[X(T)] = X(0).
Finally, the biased game shows why the martingale method is worth the care. If heads has probability q (not 1/2), the fortune itself is no longer a martingale — it is a submartingale or supermartingale that drifts. The fix is the famous product martingale: let r = (1-q)/q and watch Y(n) = r^(X(n)). One line of conditioning shows E[r^(X(n+1)) given the past] = r^(X(n)), so Y(n) is a martingale even with the bias, and the very same optional-stopping-then-two-points routine yields P(reach N) = (1 - r^a) / (1 - r^N). The difference-equation approach would have you solving a recurrence; the martingale approach just asks you to find the right thing whose average stays flat.