Expected Number of HH

Source: Placement Paper of one of the CS companies


Problem: A coin is tossed 10 times and the output written as a string. What is the expected number of HH? Note that in HHH, number of HH = 2

Example: In 3 tosses
HHH (2 HH)
HHT (1 HH)
HTH (0 HH)
HTT (0 HH)
THH (1 HH)
THT (0 HH)
TTH (0 HH)
TTT (0 HH)
Expected number of HH in 3 tosses = 2/8 + 1/8 + 1/8 = 0.5

Update (11/12/09): Solution: Posted by Asad in Comments!!

Comments

  1. 9/4?
    Because each extra coin, say ith, brings in 1/4 expected heads as i-1 coins will have 1/2 of the sequences ending in H thus 1/2*1/2(prob. of i = H) = 1/4 extra expected H
    Thus for 10 coins, (10-1)/4

    Please confirm if this is correct

    Reply Delete
  2. @asad..
    This is the answer I wrote. But we were told that the answer is wrong.

    Reply Delete
  3. I still feel that the answer is correct.Confirm it by running the following java program:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package javaapplication1;

    import java.math.*;
    import java.util.*;
    /**
    *
    * @author anshum
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    int numberOfHH = 0;
    boolean isLastFlipHead = false;
    Random r = new Random();
    for(int i = 1;i<10000000;i++)
    {
    isLastFlipHead = false;
    for(int j=0;j<10;j++)
    {
    double a = r.nextDouble();
    if(a<0.5)
    isLastFlipHead = false;
    else
    {
    if(isLastFlipHead)
    numberOfHH++;
    isLastFlipHead = true;
    }
    }
    }
    System.out.println( (double)numberOfHH/10000000.0);
    // TODO code application logic here
    }

    }


    It gives the answer as 2.25 approx

    Reply Delete
  4. jaadu rocks!! Thanx for the code.
    But I am sure he said that the answer is wrong. :(

    Will call someone in a few days and confirm.

    Reply Delete
  5. i am bad at probability, but that's exactly the solution i just thought of.

    Reply Delete
  6. Just confirmed with people who made the paper and interviewed me :P

    The answer 2.25 is correct. The solution I gave was not.

    @asad.. Your solution is correct. Thanx

    Let the expected number of HH for n tossed is E(n)

    So, probability that an n-1 toss experiments, ends in T is 1/2.

    So, E(n) = 1/2*E(n-1) + 1/2*( 1/2*(E(n-1)+1) + 1/2*(E(n-1)))
    {
    The first case when it ends in T.
    The second case when it ends in H.

    In the second case, if you get an H then, you get 1 more HH.
    }
    So, E(n) = E(n-1) + 1/4
    E(2) we know is 1/4

    So, E(n) = (n-1)/4

    For n=10, E(10) = 2.25 :)

    Thanx to Asad, Jaadu, Ramdas and Avin :)

    Reply Delete
  7. Sol Using Linearity of Expectations ::

    let X be the random variable denoting the number of HH's.

    Let X_i be the indicator random variable which takes value 1 if H's occur in ith & (i+1)th positions, and 0 otherwise.

    Note that each X_i is identically distributed, hence their expected values will be same.
    E[X_i]= Pr( X_i = 1 ) = 1/4.

    Now, X = X_1 + X_2 + .... + X_(n-1)

    By Linearity of Expectation
    E[X]=E[X_1]+E[X_2]+ ... +E[X_(n-1)]
    E[X] = (n-1) E[X_1].
    Hence, E[X] = (n-1)/4.

    Reply Delete
  8. This can be done very easily.only possible pairs in the result can be HH,HT,TH,TT.And the expected number of occurrences of these must be equal as there is no reason for precedence for one over another.Therefore ans=(n-1)/4.

    Reply Delete

Post a Comment

[フレーム]

Popular posts from this blog

Buying Dimsums

Source: Alok Goyal (Stellaris VP, Ex-Helion VC) puzzle blog Problem: A fast food restaurant sells dimsums in boxes of 7 and 3. What’s the greatest number of dimsums a person cannot buy. Generalize it for p and q where p and q are relatively prime. I loved the puzzle. Hope you enjoy it too.

Polya's Urn Problem

Puzzle: There are two urns with one ball each. Each of subsequent n-2 balls is placed into one of these urns, with probability proportional to the number of balls already in that urn. What is the expected number of balls in the smaller sized urn? Source: P. Winkler's Puzzles book. (Chapter: Probability). Solution: Highlight the part between the * symbols for the answer. * This problem can be reformulated as the following problem. Suppose I have a stack of black cards and one red card. Initially I take red card in my hand. Now I add black cards randomly between any two cards (so, initially its either above or below red). Note that the probability that I add the card above the red card, when x-1 is the number of cards above red and y-1 is the number of cards below red is x/(x+y). Let the problem be if red card is dividing the black cards into two sets, what is the expected number of black cards in the smaller section. So, we see that the two problems are equivalent. No...

(Advanced) Cheryl's Birthday Puzzle

Source: Sent to me by Prateek Chandra Jha (IIT Bombay) Problem: This problem is inspired by the Cheryl's Birthday Puzzle ( FB Post , Guardian Link ). Paul, Sam and Dean are assigned the task of figuring out two numbers. They get the following information: Both numbers are integers between (including) 1 and 1000 Both numbers may also be identical. Paul is told the product of the two numbers, Sam the sum and Dean the difference. After receiving their number, the following conversation takes place: Paul: I do not know the two numbers. Sam: You did not have to tell me that, I already knew that. Paul: Then I now know the two numbers. Sam: I also know them. Dean: I do not know the two numbers. I can only guess one which may probably be correct but I am not sure. Paul: I know which one you are assuming but it is incorrect. Dean: Ok, I also know the two numbers. What are the two numbers? Disclaimer: Its not a puzzle for 14-15 year olds like Cheryl's