Alternating coin toss – redux

I want to revisit a probability puzzle from a previous post: see Alternating coin toss game.

You play a game where you alternate tossing a coin with a friend, and the first person to toss heads wins. But let’s make it a little more interesting by making the coin weighted, or biased, so that it lands heads only 25% of the time. If you were to start, what would be your chances of winning?

Let’s call the probability of tossing heads p and the probability of tossing tails q.

On the starter’s first toss, his chances of tossing heads are p. The chances he’ll toss heads on his second toss are q^2\times p. On his 4th toss it’s q^4\times p and so on.

So his chances of winning the game is the sum s of the geometric progression p\times q^0\;+\;p\times q^2\;+\;p\times q^4\;+

Let’s use the shifting approach to solve this. The idea is to subtract out the first element, and then shift the rest of the elements to the left. So first subtract:
s-p\times q^0=p\times q^2\;+\;p\times q^4\;+

Now to shift the terms to the left, we need to divide by q^2
\frac{s-p\times q^0}{q^2}=p\times q^0\;+\;p\times q^2\;+

So…
\frac{s-p\times q^0}{q^2}=s
s=\frac{p}{1-q^2}

This gives us a general solution to the alternating coin toss problem. If we plug in the unweighted coin probabilities, we get s=\frac{2}{3} which is what we’d worked out before. For the case where the probability of tossing a head is 1/4, then we get s=\frac{4}{7}, which is the probability of winning for the starter.

Posted in puzzles | Tagged , | Leave a comment

Puzzle – Take your seats

Another problem that has an immediate intuitive answer, but can be tricky if you don’t have that insight.

Question

Let’s say there are 100 people, including you, waiting to board a fully booked flight, each with an assigned seat. The first person to board has seat #1 assigned but doesn’t pay any attention to this and sits somewhere at random. Each of the subsequent people will sit in their assigned seat if they can, but if they find someone sitting there will pick another seat at random. You’re the last to board with seat #100 assigned. What’s the probability you’ll get to sit in your assigned seat?

Answer

OK so if you’re really smart you may be starting to toy with conditional probability.

But an insight that can solve this quickly is that as soon as anyone sits in seat #1, I’ll be guaranteed my seat. Conversely, as soon as anyone sits in seat #100, my seat is eliminated and I won’t get to sit on it.  In other words, there will be one passenger that seals my fate, and all other passengers are irrelevant.

The passenger that sealed my fate had a 50/50 chance of either guaranteeing or eliminating my seat.  That applies for passenger #1, who had a 1/100 chance of choosing his own seat #1, and a 1/100 chance of choosing my seat #100.  If he’s the one that gets to seal my fate, it’s 50/50 which way he’ll do it.  If it gets to passenger #99 without my fate being sealed, #99 will have the pleasure of definitely doing so with only seats #1 and #100 left, which he’ll choose at random as neither is his.

So you can see that the chance you get to sit on your own seat is 50/50, because the passenger that sealed your fate had a 50/50 chance of doing it either way and all other passengers who chose seats other than #1 or #100 are irrelevant.  And your fate will be definitely be sealed somewhere between passenger #1 and #99.

Anybody have a different way to get the answer – or can get the answer by looking at the sum of conditional probabilities?

Posted in puzzles | Tagged , | 2 Comments

Puzzle – Leaning Tower of Pisa

Let’s say you drop a ball from the Leaning Tower of Pisa, which is 179 ft high, and it bounces back 10% of the dropped height – 17.9 ft. Then on the second bounce it bounces up 10% again – 1.79 ft, and so on for ever. How far will the ball travel?

The first thing to realize is that this is a geometric progression which will yield a finite sum, even though it has an infinite number of terms. This idea of finite sums is the reason Zeno can walk across the room: http://en.wikipedia.org/wiki/Geometric_series#Zeno.27s_paradoxes.

Before answering the question it may be useful to recap geometric progressions from my previous post see the coin-toss puzzle answer.

In this tower problem, the total distance travelled by the ball will be: 179 + 179 * 2 * (1/10 + 1/100 + 1/1000…)

You may be able to see straight away that the sum of the progression is 1/9, and so the total distance 218 + 7/9.

But let’s do it using the standard approach:

Given that n in the progression will start at 1, we need to get our progression into the form:

\sum_{n=1}^\infty cr^{n-1} = \frac{c}{1-r}

When we do this, we get the same answer as above:

distance =\;179\;+\;179\;*\;2\;*\;1/10\;*\;\frac{1}{1-\frac{1}{10}}\;=\;218\;\frac{7}{9}

Posted in mathematics, puzzles | Tagged , | Leave a comment

Puzzle – Break a stick to get a triangle

I read somewhere this is a Google interview question:

Let’s say you drop a stick, breaking it randomly in 2 places, leaving you with 3 smaller sticks.   What’s the probability you can make a triangle out of the 3 resulting sticks?

There are a number of ways to tackle this, including this very elegant solution: http://www.cut-the-knot.org/Curriculum/Probability/TriProbability.shtml. For the less elegantly inclined (and those as unlikely as me to be working at Google any time soon), here’s another way:

The first insight is that we can think of the breaks as two separate events.  The first break can happen anywhere.  To make a triangle, the second break has to happen on the opposite half from the first break. Otherwise we’d end up with one stick being more than half the original length, and we can’t make a triangle if that’s the case.  The probability of the 2nd break being on the opposite half is 50%.

But even with this first condition met, we may still end up with one stick being longer than half the original, if the two breaks are close to the ends.  We also need the sum of the distances of the two breaks from the ends to be greater than 1/2 the length of the original stick. What’s the probability of that? The probability that two random numbers from 0 to .5 add up to more than .5 is 50%

So that’s a slightly long-winded way of arriving at the answer – the 2nd break has to satisfy two independent 50% chances, so we have a 1 in 4 chance of being able to make a triangle.

Google, you have my number.

Posted in puzzles | Tagged , | Leave a comment

Puzzle – 4 Bugs chasing each other

The answer to this problem is either really obvious, or it’s not.

Say there are four bugs standing at the corners of an imaginary 1m * 1m square.  Each of the bugs is facing the bug that is on the adjacent, clockwise corner from where they are.

At the same instant, each of the four bugs starts walking directly towards the bug they are facing at 1m per hour, and keeps walking directly towards that bug as that bug itself starts moving.  So the four bugs spiral towards the center of the square in a clockwise direction.

How long does it take for the four bugs to meet?

Answer

There are two different insights one could have in thinking about this problem. 

The first insight – let’s call it the “square formation” insight is that the bugs will effectively always stay in a square formation in their path to the center. A shrinking and spiraling square, but a square that is always centered on the original center. This means that regardless of where the bugs are in their spiral towards the center, they will always be moving 45 degrees to a line connecting where they are to the center.

So one can calculate their effective velocity towards the center:

V_{center} = \cos\;45^\circ\;*\; V_{absolute}\\ V_{center} = \frac{1}{\sqrt(2)}\;m/hr

Since the total distance towards the center from the starting position is \frac{1}{\sqrt(2)}\;m, it will take the bugs 1hr to meet in the center.

Those that have the second insight – let’s call it the “perpendicular trajectory” insight – will think this problem is so obvious they’ll wonder what the fuss is about.  A bug’s movement towards the bug it is following is always perpendicular to the motion of the followed bug.  So in effect the movement of the followed bug has no effect on how far the follower has to travel to reach it or how long it will take, so the follower has to travel 1 meter, and will catch up to the followed bug in 1 hr.  A-ha!

What if the 3 bugs were in an equilateral triangles with sides 1m?

Same principle as the above:  they stay in formation as they spiral to the center.   One bug is heading towards the other at 1 meter per hour.  In this case, the followed bug is making effective progress towards the follower at  cos(60), or .5 meters per hour.   So they have a combined speed of 1.5 meters per hour towards each other, so they should cover the 1 meter between them in 40 minutes.

Posted in puzzles | Tagged , | Leave a comment

Puzzle – Liars and Truthtellers

This is another probability question that seems straightforward but can be deceptive…

You’re an FBI agent and have been monitoring a large gang, and you know that 1/4 of its members always tell the truth, the rest always lie.  Then you get taken hostage by the gang, and two gang members are assigned to guard you.  You ask the first: “Am I going to live?”  He answers “Yes.”  Then you ask the second the same question.  He also answers “Yes.”  What are your odds of living?

It can be tempting to say it’s the probability the first is telling the truth multipled by the probability the second is telling the truth, which would mean you have a 1/16th chance of living.

It’s not correct, however, because there’s another piece of information you have which is that they both gave the same answer.

Let’s look at the probability tree:

GUARD 1GUARD 2PROBABILITY
TT1/16
FF9/16
TF3/16
FT3/16

The only two rows that apply to this problem are the first two – they can either both be telling the truth, or both be lying.  The chances they’re both telling the truth is:

\frac{\frac{1}{16}}{\frac{1}{16}+\frac{9}{16}}=\frac{1}{10}

So, using the same table, what would your chances of living be if the first guard told you that you would live, but the second that you would die?

Looking at the table you’ll see that your chances are 50/50.


Posted in puzzles | Tagged , | Leave a comment

Puzzle – shortest road system

Let’s say you’re contracted to build roads to connect four towns that are at the corners of an imaginary square with sides 1 mile. What’s the shortest length of road system you can build so that every town can get to every other town?

The first answers that come to mind for most folks are either to do a perimeter road with one one side left off which is 3 miles or to build roads in the shape of an X – which pythagoras tells is would be 2 * sqrt(2) = 2.828 miles.

We can do better. Click on this link to see what the road system would look like, and the theory behind it.

To work out the total length we can use some basic calculus:

Call x the length of shared highway in the middle. So the total length of the road will be:

l\;=\;4\sqrt{(\frac{1}{2})^2 + (\frac{(1-x)}{2})^2}\;+\;x

Simplifying:

l\;=\;\sqrt{4 + 4(1-x)^2}\;+\;x

We’re trying to find x where l is the shortest. So we can differentiate l with respect to x, and set this equal to zero. Recall the chaining rule from high school?

\frac{dl}{dx}\;=\;\frac{1}{2}\;*\;(4 + 4(1-x)^2)^{-\frac{1}{2}}\;*\;8(1-x)\;*\;-1\;+\;1\;=\;0

\frac{1}{\sqrt{4 + 4(1-x)^2}}\;=\;\frac{1}{4(1-x)}

4 + 4(1-x)^2\;=\;\frac{1}{16(1-x)^2}

12(1-x)^2\;=\;4

1-x\;=\;\sqrt{\frac{1}{3}}

x\;=\;1-\sqrt{\frac{1}{3}}

And now we can substitute to find the total length of the road:

l\;=\;4 * \sqrt{\frac{1}{4} + \frac{1}{12}}\;+\;1\;-\;\sqrt{\frac{1}{3}}

l\;=\;4 * \sqrt{\frac{1}{3}}\;+\;1\;-\;\sqrt{\frac{1}{3}}

l\;=\;3 * \sqrt{\frac{1}{3}}\;+\;1

l\;=\;sqrt{3}\;+\;1

This works out to be 2.732 miles, a little better than our original X approach.

It’s neat that the angles turn out to be 120 degrees (as is shown in the link above). A colleague declared to me that this was ‘obvious’, but it’s not so obvious to me. Can anyone find a trigonometric way to solve this problem that doesn’t involve the calculus? Or at least provide an explanation for why the 120 degrees makes sense (other than that it’s a beautiful symmetry)?

Now, only because I’m a maniac and am weirdly enjoying doing high school Calculus for the first time in a while, I’m going to do this again this time picking x to be the distance from the shared highway to the edge of the square. So the shared highway length will be 1-2x.

l\;=\;4\sqrt{\frac{1}{4} + x^2}\;+\;1\;-\;2x

Simplifying:

l\;=\;\sqrt{4 + 16x^2}\;+\;1\;-\;2x

Now we differentiate:

\frac{dl}{dx}\;=\;\frac{1}{2}\;*\;(4 + 16x^2)^{-\frac{1}{2}}\;*\;32x\;=\;2

\sqrt{4 + 16x^2}\;=\;\frac{1}{8x}

4+16x^2\;=\;64x^2

4\;=\;48x^2

x\;=\;sqrt{\frac{1}{12}}

Now we can substitute for the length:

l\;=\;4 * \sqrt{\frac{1}{4} + \frac{1}{12}}\;+\;1\;-\;\sqrt{\frac{1}{3}}

Which we can see is identical to what we had above, so our length is again:

l\;=\;sqrt{3}\;+\;1

Posted in puzzles | Tagged , | 4 Comments

Puzzle – Clock

It’s 3 O’Clock.

What will be the time when the minute and hour hands are next in the same position (coincident)?

One way to solve this problem:  start with the minute hand at the 12 and hour hand at the 3. Let’s call x the number of minute markers from 12 O’clock to where the hands meet. The minute hand will take x minutes to get there (it travels a minute marker a minute). The hour hand will need to travel (x-15) minute markers and we know it will take (x-15)*12 minutes to get there. When the hands meet, the same time will have elapsed for both so we can solve for x:

x = (x-15)*12
x=12x-180
11x=180
x=180/11
x=16 and 4/11 minutes

There’s a smarter way to solve this problem:  you realize that there’s 11 intervals at which the minute hand and the hour hand coincide over the course of a 12 hour period, so each is 60/11 minute markers apart on the clock face. The one after 3 O’Clock is the third, so it’s 60*3/11 minutes after the hour, which is 180/11 minutes.

Posted in puzzles | Tagged , , | 5 Comments

More puzzles…

Some of these are from Heard on the Street – a book of interview questions for Quants I’m trying to get through. Well worth a read if you’re into this sort of thing…

1. Two ropes of different lengths, and you know that if you were to light either of them at one end it burns through in an hour. How can use use them to measure 45 mins?

Light the first of them at both ends, and at the same time the second at one end. When both lit ends of the first meet (after 30 mins), light the second end of the second rope (which should take another 15mins to meet the other lit end).

Bonus points on this one: is it a condition for this approach to work that the ropes burn at a constant rate?

2. Two identical jugs, one with water, one with vodka. You pour a bit of vodka into the water, mix it, then pour the mix back to get the jugs to their original volumes. What’s the relationship between the new concentration of vodka in the vodka jug and water in the water jug?

Like a lot of these, getting straight into the algebra is not a good idea. The key here is that the volume of vodka and water remains the same. Any that displacement of (say) vodka from the vodka jug must have been replaced by an identical amount of water – so in effect same amounts of vodka and water have just swapped places, and the concentrations are the same.

3. Imagine you’re an ant (you can walk on walls but not fly) and you want to get from a bottom corner of a cubic room (1*1*1) to the extreme opposite corner (farthest from you). What’s your shortest path to get there?

If you said walk diagonally along the floor to the opposite floor corner, then up the joint in the walls to the ceiling corner – a distance of 1 + sqrt(2), think again. Think of the room as a box that’s been laid flat, with the sides out so it’s like the cross on the swiss flag. The distance between the two corners in question is sqrt(5).

4. Let’s say you have a bunch of 1*1*1 mini-cubes that you’ve assembled into a 10*10*10 big cube. Now let’s say the whole outer layer of the big cube becomes damaged and has to be replaced – how many new mini-cubes do you need?

Two ways of doing this – the Plodders way and then a smart way. A Plodder would say you need a top and bottom layer (10*10 + 10*10 = 200) and 8 ring layers (8 * (10+10+8+8) = 288). Total 488.

The smart way is to think about the inner cube that’s left, which is 8*8*8, or 8^3. So your answer is 10^3 – 8^3. You should be able to do this in your head: 10^3 is 1000. 8^3 is the same as (2^3)^3, or 2^9. Everyone knows 2^10 which is 1024. 2^9 is half that, 512. 1000-512 is 488.

5. Say there are 100 lights with switches (initially off), and 100 people. The first person goes through and flips every switch (so all the lights are on). The second person flips every second switch, so at this point half the lights are off again. The third person every third switch, and so on. By the time the 100th person goes through he just flips switch #100. At the end, how many light bulbs are turned on?

The trick here is to understand that the number of times a switch gets flipped depends on the number of factors that switch number has. So for example switch #6 has an even number of factors – 1,2,3 and 6 – so it ends up off. You’ll see that most numbers have an even number of factors because they have product pairs (for example, 1 and 6, and 2 and 3). Which numbers don’t have product pairs? Well, they all have product pairs, but in some cases both numbers in the product pair are the same – when the number has an integer square root. So for example the product pairs for 9 are 1 and 9, and 3 and 3. So the people who flip the #9 switch are 1, 3 and 9 – an odd number that leaves the light turned on. So the lights left on are these “perfect squares” of 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100.

Posted in puzzles | Tagged , , | 2 Comments

Mth to last – a recursive approach in Python

A collegue of mine was pondering the mthToLast problem for a LinkedList I’d blogged about previously and sent me the following IM message:

just a guess - but can you use  recursion to solve your link list m problem? 
a method to track through the list which calls itself, 
then when you get to the end start incrementing a counter, 
and when it equals M return the value?

I think this is what he meant – and it seems to work. Not as scalable as the trailing pointer approach in the previous post, but an interesting exercise in recursion. You find the size at the end, and return it all the way back. Simple, but recursion can blow your mind sometimes.

class Element:
    def __init__(self,value,next=None):
        self.value=value
        self.next=next

def findMToLast(element,m,counter=1):
    """ 
    >>> e1=Element(1)
    >>> e2=Element(2)
    >>> e3=Element(3)
    >>> e4=Element(4)
    >>> e1.next=e2
    >>> e2.next=e3
    >>> e3.next=e4
    >>> findMToLast(e1,1).value
    4
    >>> findMToLast(e1,2).value
    3
    >>> findMToLast(e1,3).value
    2
    >>> findMToLast(e1,4).value
    1
    """
    if element.next==None:
        if m==1: mtolast=element
        else: mtolast=None
        if counter==1: return mtolast
        return (counter,mtolast)
    (size,mtolast)=findMToLast(element.next,m,counter+1)
    if size-counter+1==m: mtolast=element
    if counter==1: return mtolast
    return (size,mtolast)

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Posted in programming | Tagged , , , | Leave a comment