News for hackers. Stuff that matters.
Re:How to leave academia (for science PhDs) by robertskmiles on Thursday April 05, 2012 @12:03PM (#3803164)
> 100 passengers have queued up to board a plane, and are lined up in the order of the seats on the plane (n=1..100). However, the first person lost his ticket and selects a random seat. The remaining passengers will occupy their assigned seat if it is available, or a random seat otherwise. What is the probability that passenger 100 sits in seat 100?

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.

Re:How to leave academia (for science PhDs) by dxbydt on Thursday April 05, 2012 @01:28PM (#3803593)
Its 50%.

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.4997
Re:How to leave academia (for science PhDs) by lgieron on Thursday April 05, 2012 @02:33PM (#3803900)
Minor comment from a Scala fan - on that last line you could have used:

happyLast.count(_ == true)

instead of:

happyLast.filter(x=>x).size

Re:How to leave academia (for science PhDs) by graphene on Thursday April 05, 2012 @02:48PM (#3803978)
shouldn't that be {123, 213, 312, 321}?

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.

Re:How to leave academia (for science PhDs) by dxbydt on Thursday April 05, 2012 @03:26PM (#3804174)
uh, nope.

     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)
Re:How to leave academia (for science PhDs) by graphene on Thursday April 05, 2012 @03:37PM (#3804224)
ah, right, I read it as

xyz => seat 1 is occupied by passenger x, seat 2 by y and seat 3 by z

Re:How to leave academia (for science PhDs) by VMG on Thursday April 05, 2012 @01:41PM (#3803645)
I think this is more slightly more on-topic as a reply here

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

Re:How to leave academia (for science PhDs) by dxbydt on Thursday April 05, 2012 @03:45PM (#3804261)
I believe the right answer is about 2.5% (0.0247)

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....:)
Re:How to leave academia (for science PhDs) by bacr on Thursday April 05, 2012 @04:42PM (#3804522)
Nice simulation work! Initially I thought this was a one dimensional random walk problem where the answer is a function of n (as other comments have pointed out). In that case, there (n choose k) paths that take k steps in a single direction of n total steps, and each occurs with p(0.5^n). In this problem we are given n, but not k. Given a starting point, we can easily calculate the probability of crossing 0.

So my question is, how are you dealing with the initial condition?

Re:How to leave academia (for science PhDs) by dxbydt on Thursday April 05, 2012 @06:19PM (#3804942)
The initial condition is assigned to you randomly, from the closed interval [1,1000].

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.
Re:How to leave academia (for science PhDs) by theprettyllama on Sunday April 08, 2012 @02:01AM (#3813224)
I read this problem a few days ago and just decided to give it some thought now (I would love to know the correct solution).

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?

Re:How to leave academia (for science PhDs) by theprettyllama on Sunday April 08, 2012 @06:04PM (#3815056)
I posted this to /r/math on reddit and got some very interesting insight. (http://www.reddit.com/r/math/comments/ryy5n/interesting_litt...)

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.

Re:How to leave academia (for science PhDs) by mturmon on Thursday April 05, 2012 @03:02PM (#3804053)
The problem may have a simple answer, but 0.5 is not it, because the answer will be a function of n, the starting position. If n is near 0, the chance you hit zero before 1000 steps is larger.

The keyword is "random walk hitting time".

Re:How to leave academia (for science PhDs) by yummyfajitas on Thursday April 05, 2012 @01:59PM (#3803741)
The particle doesn't necessarily start out in the middle.
Re:How to leave academia (for science PhDs) by VMG on Thursday April 05, 2012 @03:36PM (#3804220)
So the question is "How likely is for a particle to reach 0 or 1000 depending on the starting position"? Then I got the question wrong, thinking it was about the average likelihood for many particles.
Re:How to leave academia (for science PhDs) by dkokelley on Thursday April 05, 2012 @01:14PM (#3803526)
I have no idea if that is correct, but my initial intuition was that it would be a high percentage like you found. However, now that I'm thinking about it, I believe the answer is closer to 50%.

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.

Re:How to leave academia (for science PhDs) by bannerts on Friday April 06, 2012 @12:25AM (#3805997)
This seems to make a lot of sense to me. It appears that in every situation starts off with a passenger randomly selecting a seat, summarized as either sitting in (a) seat 100 [Fail], (b) seat 1 [Success] or (c) some other seat [?]. Options (a) and (b) always have equal likelihoods while option (c) simply forces a later passenger to go through the same random process.
Re:How to leave academia (for science PhDs) by bearmf on Thursday April 05, 2012 @12:38PM (#3803362)
1. Wrong 2. Start with 2 passengers instead of 100 3. Rigorous proof would take a bit longer, say 15 minutes