KatInterview

Expected Flips for Random Walkers to Collide on Octagon

Math & Bit ManipulationAuto-graded · 19 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilitymarkov-chainsrandom-walks

Last 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.