# If A is distributed uniformly on [8,10] and B on [9,11], what is the probability that B<A?

I was asked this question in an interview, and did not initially answer correctly though I still think my interpretation may have been the correct one. The question was:

There are two delivery trucks, A and B. A makes deliveries between 8am and 10am, and B makes deliveries between 9am and 11am. The deliveries are uniformly distributed for both. What is the probability that any given delivery from B will take place before any delivery from A?

It's 1/8. See the figure below, which shows A's delivery time on the x-axis and B's on the y-axis. Since deliveries are uniformly distributed, all points in the square are equally likely to occur. B delivers before A only in the shaded region, which is 1/8 of the total figure.

Another way to think of it is that there's a 50% chance A delivers before B even starts, and 50% chance that B delivers after A is done, meaning there's a 75% chance of one or both of those happening. In the 25% chance they both deliver in the overlapping hour, it's a 50-50 chance of which delivers first.

• Thanks for the answer and especially the graph, and that is the answer I eventually gave (using your "another way to think about it"). But since the way the question was posed is referring only to the trips made by B, it seems to me that it's actually 1/4, since for every 100 trips made by B, 25 of them are before A in the overlapping hour. In other words, why does the portion where there was no chance that B could arrive first because B was not making any trips count? I read the question as only asking about B's trips. But my probability skills are admittedly weak after years of disuse. – JPMaverick Sep 6 '18 at 18:59
• The answer holds because it's asking about "any delivery from B" compared to "any delivery from A". Imagine a case where A delivers from 6am to 10am instead of 8am-10am. Now it's even less likely that a random delivery from B is performed before a random delivery from A. If you don't count A's deliveries that occur outside of B's hours, extending A's delivery window to be earlier would have no effect. – Nuclear Wang Sep 6 '18 at 19:29
• Perfect, got it. Thanks so much. Guess I won't be getting admitted into THAT program, lol – JPMaverick Sep 6 '18 at 19:36
• I still can't seem to let this go, because it still seems to me that A's window getting extended is irrelevant if the question is "Of all of B's trips, what percentage of them took place before at least one of A's trips". Since the distribution is uniform, the number of potentially overlapping trips taken does not change by extending A's delivery window. It seems that there is actually NO answer possible without knowing the delivery rates of both A and B, if the questions is posed only about B (as it was). Sorry for being dense, if I am, and perfectly understandable if this is getting boring – JPMaverick Sep 6 '18 at 19:52
• @JPMaverick, I agree the exact wording of the question is important. This answer works when it's worded as "any given delivery from B" and "any given delivery from A". If it compares "any given delivery from B" to "any delivery from A", we need to know how many deliveries A makes. If A makes a million deliveries between 8 and 10, it's extremely unlikely that a random delivery from B occurs before A has delivered anything. If A only makes 2 deliveries in that time, it's reasonably possible that "any given delivery from B" occurs before "any" delivery from A (i.e. the first delivery from A). – Nuclear Wang Sep 6 '18 at 19:59

Since the delivery rates are not specified, lets assume A delivers $a$ packages per hour and B delivers $b$ packages per hour. So there are $2a \cdot 2b$ pairs of delivery times. The window in which A and B overlap in deliver times has only $a \cdot b$ pairs, in half of which A comes before B. So the proportion of pairs in which A comes before B is $$\frac{a\cdot b}{2}\frac{1}{2a \cdot 2b} = \frac{1}{8}.$$

I propose another way of looking at it, only if you had a pc during the interview of course.

We can simulate the process with R, for example.

Let's simulate 1000 values from A and the same from B, we know that both are uniforms, are independent.

a <- runif(1000, 8, 10) # A deliveries
b <- runif(1000, 9, 11) # B deliveries
# [1] 9.485513 8.665070 8.488481 8.840332 8.755384 9.448949 # A deliveries for example

Ok they're not exactly times, but it's the same.

The probability $P(B<A)$ is what we seek. So we just count the number of pairs where $b<a$ in our code.

prob <- sum(b < a)/1000
#[1] 0.112 # almost 1/8

We can also plot the 1000 pairs $(a,b)$, and see the region where B comes first.

plot(a, b)
polygon(c(9, 10, 10, 9),
c(9, 9, 10, 9), density = 10, angle = 135)

And the prob value above is the proportion of points in the shaded region (looks familiar doesn't it?).

Now we could use the formula for the standard error of a proportion to estimate the standard error of the simulation.

se <- sqrt(prob * (1 - prob) / 1000)
#[1] 0.009972763

And we can build a CI (assuming Normal approximation of the sample distribution of probs).

prob - 1.96*se
#[1] 0.09245338 lower bound
prob + 1.96*se
#[1] 0.1315466 upper bound

Stumbled across this and it got in my head. :-)

The answer seems like it must depend on the relative number of deliveries each truck makes in the hour of possible overlap (9a-10a) -- there's no constant answer.

For example, suppose each truck makes 2 total deliveries (1 per hour). They'd each make 1 delivery between 9 and 10 and B wouldn't beat anything from A. So, the probability is 0 in that case.

Consider a simplified version of the problem where they both only make deliveries between 9-10a (still a uniform distribution). And, for starters, suppose they make the same number of deliveries, n.

• The first delivery for B will beat everything except the first delivery from A (which it ties). So, with probability $$\frac{1}{n}$$ (the probability we're the first delivery for B) we beat an event with probability $$\frac{n-1}{n}$$ (the probability we're not the first delivery of A)
• The second delivery for B will beat everything except the first two deliveries from A. So, with probability $$\frac{1}{n}$$ we beat an event with probablity $$\frac{n-2}{n}$$
• etc.

Putting each of those terms into a summation, we get:

$$(\frac{1}{n} \cdot \frac{n-1}{n}) + (\frac{1}{n} \cdot \frac{n-2}{n}) + ... + (\frac{1}{n} \cdot \frac{n-n}{n})$$

Or,

$$\sum_{i=1}^{n} \frac{n-i}{n^2}$$

Since the probabilities are uniform and half (rounded down) of each occur during the hour of overlap, we only consider half the deliveries of each. If $$n'=\lfloor\frac{n}{2}\rfloor$$ and, compared to the whole domain, those events only happen half the time. So

$$\frac{1}{2}\sum_{i=1}^{n'} \frac{n'-i}{n'^2}$$

I believe that for $$a=b=n$$, you get $$1/8$$.

How to handle the fact A and B do not deliver the same number of packages? Again, to simplify, assume all their deliveries happen between 9-10am.

For every delivery $$b$$ you consider from earliest to latest, instead of each successive one beating $$\frac{1}{a}$$ less from truck A, as above (where $$a$$ is the number of deliveries made by truck A and $$b$$ the number of deliveries made by $$b$$), you eliminate $$\lfloor \frac{1}{b} \cdot a \rfloor$$. That is, you beat all but a fraction of $$a$$ proportional to the fraction of $$b$$ you've thrown out. So,

$$(\frac{1}{b} \cdot \frac{a - \lfloor 1 \cdot \frac{a}{b} \rfloor }{a}) + (\frac{1}{b} \cdot \frac{n - \lfloor 2 \cdot \frac{a}{b} \rfloor }{a}) + ... + (\frac{1}{b} \cdot \frac{a - \lfloor a \cdot \frac{a}{b} \rfloor }{a})$$

Or,

$$\sum_{i=1}^{b} \frac{a - \lfloor \frac{ia}{b} \rfloor }{ab}$$

Again, accounting for the fact that they only overlap half the time, let $$a'=\frac{a}{2}$$ and $$b'=\frac{b}{2}$$:

$$\frac{1}{2}\sum_{i=1}^{b'} \frac{a' - \lfloor \frac{ia'}{b'} \rfloor }{a'b'}$$

• The question asks for a probability. It is difficult to find any realistic values of $a,b,$ and $n$ for which your formulas produce results between $0$ and $1,$ so they must be wrong. As other respondents have shown, there is a unique answer. It derives from the assumption of a uniform probability distribution for the delivery time for each truck. – whuber Sep 10 '18 at 22:42

It is zero:

if the B truck has at least one delivery in the period of [9-11] at least one delivery is made after (or equal to ) 10

and that delivery is not before the deliveries of A (which are all before 10)