- Joined
- 9/7/07
- Messages
- 220
- Points
- 28
Quote:
(Another interview question)
You have N cars that are all traveling the same direction on an infinitely long one-lane highway. Unfortunately, they are all going different speeds, and cannot pass each other. Eventually the cars will clump up in one or more traffic jams.
In terms of N, what is the expected number of clumps of cars?
Solution (2):
***Please see Solution (1) provided previously. It is messier and much longer.***
It is understood that although the word "jam" is used in the statement of the problem, the cars never come to a stop. What happens is that when a faster-moving car reaches a slower-moving car, then both cars move at the speed of the slower-moving car. Note that the actual speed of the cars is immaterial to the solution; what matters is that given any two distinct cars, one of them moves slower than the other.
We translate the above problem to an equivalent one as follows:
Let X(1), X(2), ...,X(N) be a sequence of random variables over the set {1,2,...,N}, with X(i) different form X(j) if i is different from j. Here X(i) represents the speed of car i, and the set {1,2,...,N} represents the speeds of the cars. We can suppose that the cars, represented by C(1),C(2),...,C(N), are in the order given and move in the East-West (Left To Right) direction. So, if X(N)=N, assuming N>1, then there will be more than one clump as none of the first (N-1) cars moving behind car C(N) can catch up with C(N) that has the greatest speed; namely N. If X(1)=N, then the number of clumps will be determined by cars C(2) through C(N) because car C(1), having the greatest speed, will sooner or later catch up with car C(2) and both will have the same speed as Car (2).
Clearly P{X(N)=N}=(1/N) and P{X(N)<N}=(N-1)/N=1-(1/N). If X(N)=N, then car C(N) will form a single-car clump by itself as none of the first (N-1) cars can catch up with car C(N). So, the total number of clumps in this situation will be 1 plus the number of clumps formed by the first (N-1) cars. If X(N)<N, then the car, say C(j) for some j, that has speed X(j)=N, will sooner or later catch up with car C(j+1), and will both have the speed of car C(j+1). In this situation, we will effectively have (N-1) cars with different speeds, and so the number of clumps will be determined by (N-1) cars.
Now let E(N) denote the expected number of clumps of N cars.
It is obvious that E(0)=0.
E(N) = (P{X(N)=N}) * E[N | X(N)=N] + (P{X(N)<N}) * E[N | X(N)<N],
E(N) = (P{X(N)=N})*(1+E(N-1)) + (P{X(N)<N})*(E(N-1)),
which can be written as
E(N) = (1/N)*(1+E(N-1)) + (1-(1/N))*(E(N-1)),
which can be simplified to
E(N) = (1/N) + E(N-1),
which of course is the Harmonic series
E(N) = (1)+(1/2)+(1/3)+...+(1/N).
(Another interview question)
You have N cars that are all traveling the same direction on an infinitely long one-lane highway. Unfortunately, they are all going different speeds, and cannot pass each other. Eventually the cars will clump up in one or more traffic jams.
In terms of N, what is the expected number of clumps of cars?
Solution (2):
***Please see Solution (1) provided previously. It is messier and much longer.***
It is understood that although the word "jam" is used in the statement of the problem, the cars never come to a stop. What happens is that when a faster-moving car reaches a slower-moving car, then both cars move at the speed of the slower-moving car. Note that the actual speed of the cars is immaterial to the solution; what matters is that given any two distinct cars, one of them moves slower than the other.
We translate the above problem to an equivalent one as follows:
Let X(1), X(2), ...,X(N) be a sequence of random variables over the set {1,2,...,N}, with X(i) different form X(j) if i is different from j. Here X(i) represents the speed of car i, and the set {1,2,...,N} represents the speeds of the cars. We can suppose that the cars, represented by C(1),C(2),...,C(N), are in the order given and move in the East-West (Left To Right) direction. So, if X(N)=N, assuming N>1, then there will be more than one clump as none of the first (N-1) cars moving behind car C(N) can catch up with C(N) that has the greatest speed; namely N. If X(1)=N, then the number of clumps will be determined by cars C(2) through C(N) because car C(1), having the greatest speed, will sooner or later catch up with car C(2) and both will have the same speed as Car (2).
Clearly P{X(N)=N}=(1/N) and P{X(N)<N}=(N-1)/N=1-(1/N). If X(N)=N, then car C(N) will form a single-car clump by itself as none of the first (N-1) cars can catch up with car C(N). So, the total number of clumps in this situation will be 1 plus the number of clumps formed by the first (N-1) cars. If X(N)<N, then the car, say C(j) for some j, that has speed X(j)=N, will sooner or later catch up with car C(j+1), and will both have the speed of car C(j+1). In this situation, we will effectively have (N-1) cars with different speeds, and so the number of clumps will be determined by (N-1) cars.
Now let E(N) denote the expected number of clumps of N cars.
It is obvious that E(0)=0.
E(N) = (P{X(N)=N}) * E[N | X(N)=N] + (P{X(N)<N}) * E[N | X(N)<N],
E(N) = (P{X(N)=N})*(1+E(N-1)) + (P{X(N)<N})*(E(N-1)),
which can be written as
E(N) = (1/N)*(1+E(N-1)) + (1-(1/N))*(E(N-1)),
which can be simplified to
E(N) = (1/N) + E(N-1),
which of course is the Harmonic series
E(N) = (1)+(1/2)+(1/3)+...+(1/N).