For 3 passengers the seating possibilities are {123,213,231,321}, so 3 gets to sit on seat 3 50% of the time.
For 4 passengers the seating possibilities are {1234,2134,2314,2341,2431,3241,3214,4231}, so 4 gets to sit on seat 4 50% of the time. From there you can do a proof by induction.
If you like code: ----
import util.{Random=>rng}
import collection.mutable.ArrayBuffer
object passengers {
def main(args:Array[String]) = {
val n = args(0).toInt
val simulations = args(1).toInt
val happyLast = (1 to simulations).map(_=> {
val seats = ArrayBuffer.fill[Int](n+1)(0)
val taken = new ArrayBuffer[Int]
val all = (1 to n).toSeq
val seat = rng.shuffle(all).head
seats(seat) = 1
taken += seat
(2 to n).foreach( p=> {
val seat = seats(p) match {
case 0=> p
case x=> rng.shuffle(all.diff(taken)).head
}
seats(seat) = p
taken += seat
})
seats(n) == n
})
println( happyLast.filter(x=>x).size*1.0d/simulations )
}
}
-----
>scala -cp classes passengers 100 100000
>0.4997happyLast.count(_ == true)
instead of:
happyLast.filter(x=>x).size
ie 231 is not possible since after 1 randomly decides to sit at 3, 2 has no reason to go sit in seat 1.
This doesn't change the answer to the problem though I think.
xyz => 1 sits on seat x, 2 on seat y, 3 on seat z
So the only possibilities are:
123: 1 sits on seat 1, 2 on seat 2, 3 on seat 3
213: 1 sits on seat 2, 2 on seat 1, 3 on seat 3
231: 1 sits on seat 2, 2 on seat 3, 3 on seat 1
321: 1 sits on seat 3, 2 on seat 2, 3 on seat 1
passenger 3 is happy twice out of 4, hence 50%
In the code, the happyLast variable counts the number of times the last passenger is happy ( happy == when he gets his assigned seat)xyz => seat 1 is occupied by passenger x, seat 2 by y and seat 3 by z
> Consider the integers from [0,1000]. Suppose a particle starts at position n. At discrete instants of time t=0,1,2,…, the particle moves up or down with p=0.5. What is the probability that the particle reaches 0 before t=1000?
The answer is 50%, right? I can't explain it properly, but my thinking is that the random walk is symmetric and 0 and 1000 should have equal weights. Maybe a good explanation is harder than the correct answer in this case.
Here's some imperative scala:
object particle {
def main(args:Array[String]) = {
val simulations = args(0).toInt
val rng = new util.Random
val reachedZero = (1 to simulations).map(_=> {
var t = 0
var z = 1+rng.nextInt(1000)
do {
z += {if (rng.nextBoolean) 1 else -1}
t +=1
} while( z != 0 && t<1000)
z==0
})
println( reachedZero.count(_==true)*1.0d/simulations )
}
}
---
>scala particle 1000000
>0.024782
Some intuition: If there were only 2 positions {1,2}, and you picked 1 with probability 0.5, you could get to 0 by going down once with probability 0.5. Going up wouldn't help since you'd have to go down to be back at 1, at which point you've exhausted your chances. Suppose you picked 2 with probability 0.5, you'd have to go down twice consecutively to hit 0, which has a probability of 0.5^2. So overall, your probability is 0.5^2 + 0.5^3 = 0.375
You can try working it out with 3 positions & 3 turns, and suddenly its not so easy....:)So my question is, how are you dealing with the initial condition?
Suppose there were [1,k] spots. You are assigned an intial value from [1,k] and you then have to reach 0 before time t=k. Here's my code to solve the general case
object particle {
def main(args:Array[String]) = {
val simulations = args(0).toInt
val rng = new util.Random
(2 to 1000).foreach(spots=>{
val reachedZero = (1 to simulations).map(_=> {
var t = 0
var z = 1+rng.nextInt(spots)
do {
z += {if (rng.nextBoolean) 1 else -1}
t +=1
} while( z != 0 && t<spots)
z==0
})
val prob = reachedZero.count(_==true)*1.0d/simulations
printf("%4d spots: %.4f\n",spots,prob )
})
}
}
>scala particle 1000000
2 0.3773
3 0.329
4 0.292
5 0.2714
6 0.2624
7 0.2428
8 0.2247
9 0.222
10 0.2157
....
So your chances drop from 37% to 21% as the interval expands to 10 spots. At 100 spots, its 8%. By 1000 spots, you have a meager 2.5% chance of crossing 0 before 1000 seconds.The answer will certainly be a function of n (because, for example, I could give you n = 1001 and that should have 0 probability whereas if I were to give you n = 1, that would have a very high probability).
My first thought was to write out a recurrence relation f of n and k (where n is the current level and k is the number of moves left). From this, we get: f(n, k) = .5f(n-1, k-1) + .5f(n+1, k-1) with f(n, k) = 0 when n > k and f(0, k) = 1. So to solve this, we just take f(n, 1000) = [a rapidly exploding function].
Any thoughts?
It appears that a basic knowledge of stochastic processes is required to actually be able to solve this problem, so it's one of those things that are actually much more complicated than they first appear.
The keyword is "random walk hitting time".
When passenger 100 begins to board, there are only two possible configurations he can find the seats in: seat 1 available or seat 100 available. If Passenger 1 sits in seat 100, then passengers 2-99 will sit in their correct seats, leaving only seat 1. If passenger 1 sits in seat 1, passengers 2-99 will sit correctly and leave only seat 100 for passenger 100. If passenger 1 sits anywhere else (say, seat 50), passengers 2 - 49 will fill correctly, passenger 50 will have to sit in seat 1 or 51-100. The moment a passenger randomly selects seat 1 or 100, the remaining passengers will sit correctly (except for passenger 100). It is impossible for any seats other than 1 or 100 to be left for passenger 100 to select.
From looking at it, I don't believe there is any bias for the end configuration to leave seat 100 open more than seat 1, and I believe that in an interview situation, you would be expected to intuit this by "talking it out". If I'm right (and I could still be very wrong), this is more of a probabilistic brainteaser than a programming interview question.
It really worried me that I couldn't figure out how to work that out. I thought about what I'd do in an interview if I was asked that, and I figured (if I had my laptop) I'd write some code. So I gave myself 5 minutes by the clock, and wrote a little python program that simulated the situation and counted the results. I ran 5 million iterations, which with pypy took 59 seconds. The number I got out was a 97.335% chance of person 100 being in seat 100.
I have 3 questions:
1. Is that the right answer?
2. How are you supposed to work it out?
3. Would working it out the proper way take less than 6 minutes?
Edit: It seemed like too high a number, which is part of why I asked. I looked through my code and found a dumb error - I forgot to remove the seat from the 'available' list if the person finds their assigned seat. That's what happens when you write code on a 5 minute deadline. Now I'm consistently getting 49.9%, which seems more reasonable.