Expected Wait Time for Repeating Train Schedules
Arrays & HashingAuto-graded · 15 test casesLast updated
Calculate and compare expected waiting times for transit services with different repeating gap patterns using length-biased sampling.
You are analyzing two transit services to determine which offers shorter expected waiting times for passengers who arrive at random times without checking the schedule.
Each service runs on a repeating cycle defined by a sequence of gaps (in minutes) between consecutive arrivals. For example, a service with gaps [5, 10, 15] repeats a pattern where trains arrive 5 minutes apart, then 10 minutes apart, then 15 minutes apart, then the cycle repeats.
A passenger arrives at a uniformly random time during the operating cycle, independent of the schedule. Calculate the expected waiting time for each service and determine which one offers a shorter expected wait.
Task
Write a function that takes two lists of positive integers representing the gap sequences for Service A and Service B, and returns:
- The expected waiting time for Service A
- The expected waiting time for Service B
- Which service is preferable (has shorter expected wait), or "Equal" if they are the same
Key Insight
When a passenger arrives uniformly at random over a repeating cycle, longer gaps are more likely to be encountered than shorter gaps—this is known as length-biased sampling or the inspection paradox. A gap of length g occupies g units of time in the cycle, so the probability of arriving during that gap is proportional to its length, not to the count of gaps.
Constraints
- Each gap list contains between 1 and 100 positive integers
- Each gap value is between 1 and 1000 minutes
- Services repeat indefinitely
- Passenger arrival is uniformly distributed over the cycle duration
- Trains depart instantly (zero dwell time)
- Return expected waits as floats; use exact string matching for the preferable field
Examples
Example 1
Input: compare_train_waits([10], [5, 10, 15])
Output: {'wait_a': 5, 'wait_b': 5.833333333333333, 'preferable': 'A'}
Hints
Hint 1
For a fixed interval of length g, a uniformly arriving passenger waits g/2 minutes on average—but which intervals does the passenger encounter?
Hint 2
Longer gaps take up more time in the cycle. The probability of arriving during a gap is proportional to the gap's length, not the number of gaps.
Hint 3
The formula involves the sum of squared gaps divided by twice the total cycle time. Derive this by weighting each gap's expected wait by its time proportion.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires understanding length-biased sampling, also called the inspection paradox. When a passenger arrives at a random time, they are more likely to encounter longer gaps simply because those gaps occupy more of the timeline.
Step-by-step reasoning
-
Cycle structure: Each service repeats a pattern of gaps. The total cycle duration is
T = sum(gaps). -
Arrival probability: A passenger arriving uniformly over the cycle has probability
g_i / Tof landing in gapi, because that gap occupiesg_itime units out ofTtotal. -
Waiting time within a gap: If the passenger arrives uniformly within a gap of length
g_i, the expected wait until the next train isg_i / 2(average of waiting times from 0 tog_i). -
Overall expected wait: Using the law of total expectation:
E[Wait] = Σ P(arrive in gap i) × E[Wait | in gap i] = Σ (g_i / T) × (g_i / 2) = Σ(g_i²) / (2T) -
Comparison: Compute this formula for both services and determine which is smaller.
Why this is correct
The key insight is that gaps are sampled proportionally to their length, not their count. A service alternating between 1-minute and 100-minute gaps spends most of its time in the long gap, so passengers overwhelmingly experience the long gap's expected wait of 50 minutes, not the average of 0.5 and 50.
For a uniform service (all gaps equal to g), the formula reduces to g²/(2g) = g/2, which matches intuition.
Complexity
- Time: O(n + m) where n and m are the lengths of the gap lists (one pass to sum, one to square-and-sum)
- Space: O(1) additional space
Solution
from typing import List, Dict
def compare_train_waits(gaps_a: List[int], gaps_b: List[int]) -> Dict[str, object]:
"""
Compare expected waiting times for two train services with repeating gap patterns.
For a service with gaps g_1, g_2, ..., g_n that repeat indefinitely:
- Cycle duration T = sum of all gaps
- Probability of arriving in gap g_i = g_i / T (length-biased)
- Expected wait within gap g_i = g_i / 2 (uniform arrival)
- Overall expected wait = sum(g_i / T * g_i / 2) = sum(g_i^2) / (2T)
"""
def compute_expected_wait(gaps: List[int]) -> float:
cycle_duration = sum(gaps)
squared_sum = sum(g * g for g in gaps)
return squared_sum / (2 * cycle_duration)
wait_a = compute_expected_wait(gaps_a)
wait_b = compute_expected_wait(gaps_b)
if wait_a < wait_b:
better = "A"
elif wait_b < wait_a:
better = "B"
else:
better = "Equal"
return {
"wait_a": wait_a,
"wait_b": wait_b,
"preferable": better
}