Expected Flips for Random Walkers to Collide on Octagon
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Tests Markov chain modeling, symmetry reduction, and first-step recurrence equations for a two-particle random walk problem.
Two tokens begin at opposite corners of a regular octagon (separated by exactly 4 edges). Each round, you flip one fair coin per token: heads advances the token one vertex clockwise, tails moves it one vertex counterclockwise. Rounds continue until both tokens land on the same vertex.
Return the expected total number of coin flips needed for the tokens to collide.
Constraints
- The octagon has 8 vertices numbered 0 through 7 in clockwise order.
- Tokens start at vertices 0 and 4.
- Each round flips exactly 2 coins (one per token) simultaneously.
- The tokens meet when they occupy the same vertex after all moves in a round are resolved.
- Movement wraps around the octagon (from vertex 7, clockwise goes to vertex 0).
- Return the expected value as an integer.
Examples
Example 1
Input: collision_flips()
Output: 16
Example 2
Input: collision_flips()
Output: 16
Example 3
Input: collision_flips()
Output: 16
Hints
Hint 1
Model the problem using the relative distance between tokens rather than tracking both positions independently.
Hint 2
Notice that the relative distance changes by -2, 0, or +2 each round depending on whether the tokens move in the same or opposite directions.
Hint 3
Set up recurrence equations for the expected number of rounds from each reachable distance state, then convert rounds to total flips.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Rather than tracking both tokens independently, we model the relative distance between them as a single Markov chain. This drastically simplifies the problem.
Label the octagon vertices 0 through 7. Let the distance D = (position of token B − position of token A) mod 8. Initially D = 4 (opposite vertices). The tokens collide when D = 0.
Each round, both tokens move ±1 independently. The relative distance changes by:
- +2 with probability 1/4 (one moves clockwise, the other counterclockwise in the diverging direction)
- −2 with probability 1/4 (both move toward each other)
- 0 with probability 1/2 (both move in the same rotational direction)
Because D starts at 4 (even) and changes only by 0 or ±2, the distance remains even throughout: D ∈ {0, 2, 4, 6}. By circular symmetry on the octagon, distances 2 and 6 are equivalent (both represent being 2 edges apart).
We set up recurrence equations for the expected number of rounds R_d from distance d:
From distance 4:
R_4 = 1 + (1/2)·R_4 + (1/2)·R_2
The 1/2 coefficient for R_2 covers both the +2 and −2 transitions (each with probability 1/4).
Solving: (1/2)·R_4 = 1 + (1/2)·R_2, so R_4 = 2 + R_2.
From distance 2:
R_2 = 1 + (1/2)·R_2 + (1/4)·0 + (1/4)·R_4
Substituting R_4 = 2 + R_2:
R_2 = 1 + (1/2)·R_2 + (1/4)·(2 + R_2)
(1/2)·R_2 = 1 + 0.5 + (1/4)·R_2
(1/4)·R_2 = 1.5
R_2 = 6
Therefore R_4 = 2 + 6 = 8 rounds.
Since each round flips exactly 2 coins (one per token), the expected total number of flips is 2 × 8 = 16.
Complexity
- Time: O(1) — closed-form calculation
- Space: O(1) — constant storage
Solution Code
def collision_flips() -> int:
# Model relative distance as a lazy random walk with step size 2.
# Starting distance is 4, and we solve the recurrence:
# R_4 = 2 + R_2, R_2 = 6 by first-step analysis.
# Expected rounds from distance 4 is 8.
# Each round uses 2 coin flips.
return 16