KatInterview

Random Transfer Between Two Queues: Drain Time and Odds

Arrays & HashingAuto-graded · 24 test cases
HardAuto-gradedArrays & Hashingmarkov-chainsrandom-walkprobability

Last updated

Tests ability to model a lazy symmetric random walk as a Markov chain, derive a closed-form hitting-time formula, and compute absorption probabilities analytically.

Problem Description

Two token piles, Pile A and Pile B, together hold exactly n tokens at all times. At the start, Pile A contains k tokens and Pile B contains n − k tokens.

Each round proceeds as follows:

  1. Pick a source pile uniformly at random from {A, B}.
  2. Independently pick a destination pile uniformly at random from {A, B}.
  3. If the two choices differ, move one token from the source pile to the destination pile.
  4. If the two choices are the same, no token moves — but the round still counts.

The process stops the moment either pile reaches exactly 0 tokens.

You must compute two quantities:

  • Expected rounds: the expected number of rounds until one pile is empty, starting from k tokens in Pile A.
  • Drain probability: the probability that Pile A drains first (reaches 0 before Pile B does).

Return both values in a dictionary with keys "expected_rounds" and "probability_line_a_empties_first".

Deriving the answers

Step 1 — State space. Because the total is conserved, the system state is fully described by k, the current number of tokens in Pile A. The state space is {0, 1, …, n}, with 0 and n as absorbing states (Pile A empty and Pile B empty, respectively).

Step 2 — Transition probabilities. Each of the four (source, destination) pairs is equally likely (probability 1/4 each):

Source → DestinationProbabilityEffect on k
A → B1/4k − 1
B → A1/4k + 1
A → A1/4no change
B → B1/4no change

So from any interior state 1 ≤ k ≤ n − 1, the walk moves left or right with probability 1/4 each and stays put with probability 1/2. This is a lazy symmetric random walk.

Step 3 — Expected rounds recurrence. Let E[k] be the expected number of rounds to absorption starting from state k. By first-step analysis:

E[k] = 1 + (1/4) E[k−1] + (1/4) E[k+1] + (1/2) E[k],   for 1 ≤ k ≤ n−1

with boundary conditions E[0] = 0 and E[n] = 0.

Rearranging (subtract (1/2)E[k] from both sides, multiply by 4):

E[k+1] − 2E[k] + E[k−1] = −4

Step 4 — Solving the recurrence. The homogeneous part has solution a + bk; a particular solution for the constant right-hand side is −2k² (verify: −2(k+1)² + 4k² − 2(k−1)² = −4). The general solution is:

E[k] = −2k² + a + bk

Applying E[0] = 0 gives a = 0. Applying E[n] = 0 gives b = 2n. Therefore:

E[k] = 2k(n − k)

The factor of 2 relative to the classic gambler's ruin result k(n − k) comes from the self-loop: the walk only steps half the time, so each effective move costs 2 rounds on average.

Step 5 — Drain probability. Let p[k] be the probability that Pile A drains first starting from state k. First-step analysis gives:

p[k] = (1/2) p[k−1] + (1/2) p[k+1],   with p[0] = 1, p[n] = 0

The self-loop terms cancel exactly, leaving a harmonic equation whose solution is linear in k:

p[k] = (n − k) / n

Pile A is more likely to drain first when it starts with fewer tokens than Pile B.

Edge cases

  • When k = 0, Pile A is already empty: expected_rounds = 0 and probability_line_a_empties_first = 1.0.
  • When k = n, Pile B is already empty: expected_rounds = 0 and probability_line_a_empties_first = 0.0.
  • When k = n/2 (equal piles), the drain probability is exactly 0.5 by symmetry.

Constraints

  • 1 ≤ n ≤ 10^6
  • 0 ≤ k ≤ n
  • Both n and k are integers.
  • The return value must be a dict with exactly the keys "expected_rounds" (integer or float) and "probability_line_a_empties_first" (float).

Examples

Example 1

Input: pile_drain_metrics(13, 6)

Output: {'expected_rounds': 84, 'probability_line_a_empties_first': 0.5384615384615384}

Example 2

Input: pile_drain_metrics(2, 1)

Output: {'expected_rounds': 2, 'probability_line_a_empties_first': 0.5}

Example 3

Input: pile_drain_metrics(10, 0)

Output: {'expected_rounds': 0, 'probability_line_a_empties_first': 1}

Example 4

Input: pile_drain_metrics(10, 10)

Output: {'expected_rounds': 0, 'probability_line_a_empties_first': 0}

Hints

Hint 1

The total number of tokens never changes, so one integer — how many tokens are currently in Pile A — is all you need to track the system state. What does that tell you about the size of the state space?

Hint 2

Write the first-step equation for E[k], then move the E[k] self-loop term to the left side. You should end up with a second-difference equation with a constant right-hand side — a form you can solve exactly with a particular solution of the form ck².

Hint 3

The self-loops affect expected time but not which pile drains first. For the drain probability, the self-loop terms cancel, leaving a linear harmonic equation whose solution you can read off directly from the boundary conditions.