KatInterview

Expected Transit Wait Time with Stochastic Delays

Math & Bit ManipulationAuto-graded · 18 test cases
EasyAuto-gradedMath & Bit Manipulationmathprobabilitystatistics

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