# Ants on a Triangle

Source: https://www.techinterview.org/post/521392835/ants-on-a-triangle/
Updated: 2026-04-16 · techinterview.org

There are three ants on a triangle, one at each corner. at a given moment in time, they all set off for a different corner at random. what is the probability that they don’t collide?

### Solution

Consider the triangle ABC. We assume that the ants move towards different corners along the edges of the triangle.

Total no. of movements: 8 A->B, B->C, C->A A->B, B->A, C->A A->B, B->A, C->B A->B, B->C, C->B A->C, B->C, C->A A->C, B->A, C->A A->C, B->A, C->B A->C, B->C, C->B

Non-colliding movements: 2 A->B, B->C, C->A A->C, B->A, C->B

(i.e. the all ants move either in the clockwise or anti-clockwise direction at the same time)

P(not colliding) = 2/8 = 0.25

## 2026 Update: Symmetry, Probability, and Collision Problems

Ants-on-a-triangle problems (each ant chooses a direction randomly — what is the probability no two ants collide?) test probability over symmetric configurations. The key insight: ants collide if and only if they don't all move in the same direction. For a triangle, P(no collision) = 2/2^3 = 1/4.

**General formula:** For n ants on an n-gon, each choosing clockwise or counterclockwise with probability 0.5, P(no collision) = 2 / 2^n.


```
def prob_no_collision(n_ants):
    """Probability that n ants on an n-gon don't collide."""
    # Only 2 configurations out of 2^n work: all clockwise or all counterclockwise
    return 2 / (2 ** n_ants)

for n in [3, 4, 5, 10]:
    print(f"{n} ants: P(no collision) = {prob_no_collision(n):.6f}")
```


**The deeper insight — expected number of collisions:** Use linearity of expectation. The expected number of colliding pairs is n * P(one pair collides) = n * (1 - P(they move apart)) — calculable without enumerating all configurations.

**Modern application:** Collision-avoidance probability calculations appear in distributed systems (two writes to the same key simultaneously), robotics (multi-robot path planning), and network packet collision analysis in CSMA/CD protocols. The symmetry argument is the same in all cases.
