Expected Transit Wait Time with Stochastic Delays
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Computes the expected waiting time for a passenger arriving at a random moment at a stop with probabilistic bus delays.
Buses on a route are scheduled to arrive at a stop at regular intervals of $x$ minutes. For each scheduled bus, independently, the bus physically arrives on schedule (delay of $0$) with probability $1/2$, or arrives delayed by exactly $x/2$ minutes with probability $1/2$. No bus arrives early.
A commuter arrives at the bus stop at a point in time chosen uniformly at random over a long time horizon, independent of the schedule and delay realizations. Upon arriving, the commuter waits for and boards the very next bus that physically stops.
Write a function that calculates the commuter's expected waiting time in minutes as a function of the scheduled interval $x$.
Constraints
- $x \ge 0.0$
- The output should be a floating-point number representing the expected wait time.
Examples
Example 1
Input: expected_waiting_time(10)
Output: 5.625
Example 2
Input: expected_waiting_time(1)
Output: 0.5625
Example 3
Input: expected_waiting_time(7.5)
Output: 4.21875
Hints
Hint 1
Think about the gap lengths between successive physical bus arrivals.
Hint 2
A random arrival time is more likely to fall into a longer gap than a shorter one (this is known as the inspection paradox).
Hint 3
Calculate the distribution of gap lengths $G$, then apply the steady-state renewal formula $E[W] = \frac{E[G^2]}{2 E[G]}$ or compute the expected wait directly by conditioning.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To compute the expected wait time, we can analyze the distribution of intervals (gaps) between consecutive physical bus arrivals.
Let $D_i$ be the delay of the $i$-th scheduled bus, where $D_i \in {0, x/2}$, each with probability $1/2$. The actual arrival time of bus $i$ is $i \cdot x + D_i$. The gap between the $i$-th and $(i+1)$-th bus arrivals is:
$$G = (i+1)x + D_{i+1} - (i \cdot x + D_i) = x + D_{i+1} - D_i$$
Since $D_{i+1}$ and $D_i$ are independent random variables:
- $G = x/2$ when $(D_i, D_{i+1}) = (x/2, 0)$, with probability $1/4$.
- $G = x$ when $D_i = D_{i+1}$, with probability $1/2$.
- $G = 3x/2$ when $(D_i, D_{i+1}) = (0, x/2)$, with probability $1/4$.
Because a passenger arriving at a random point in time is sampled uniformly across time, the probability of landing in a gap of length $g$ is proportional to $g$ (the inspection paradox). Given that a passenger lands in a gap of length $g$, the expected remaining wait time is $g/2$.
Using the renewal theory result for expected residual life:
$$E[W] = \frac{E[G^2]}{2 E[G]}$$
First, compute the first two moments of the gap length $G$:
- $E[G] = \frac{1}{4}\left(\frac{x}{2}\right) + \frac{1}{2}(x) + \frac{1}{4}\left(\frac{3x}{2}\right) = x$
- $E[G^2] = \frac{1}{4}\left(\frac{x}{2}\right)^2 + \frac{1}{2}(x^2) + \frac{1}{4}\left(\frac{3x}{2}\right)^2 = \frac{x^2}{16} + \frac{x^2}{2} + \frac{9x^2}{16} = \frac{18x^2}{16} = \frac{9x^2}{8}$
Plugging these into the expected wait time formula gives:
$$E[W] = \frac{9x^2 / 8}{2x} = \frac{9x}{16}$$
Complexity
- Time Complexity: $O(1)$, as the expression is evaluated in constant arithmetic operations.
- Space Complexity: $O(1)$, using no additional space.
def expected_waiting_time(x: float) -> float:
return 9.0 * x / 16.0