This was a question at a programming contest that finished yesterday at interviewstreet:
Alice and Bob play a game. The operations at round i (i >= 1) is as follows:
- Alice pays Bob 2 * i – 1 dollars,
- Alice tosses a biased coin,
- If the result of the coin was heads for k consecutive rounds, the game stops, otherwise the game continues.
Given k and the probablity that the outcome of a toss is heads (p), your program should find the expected number of dollars Alice pays Bob, and also the expected number of rounds played.
Input
First line of input contains number of test-cases (T <= 50). Each of
the next T lines contain p and k separated by a single space. p is a
decimal number with at most two digits after the decimal point such
that 0.6 <= p <= 1. k is a positive integer such that 0 < k <= 20.
Output
For each test-case, print two integer numbers. First number is the
integer part of the expected number of rounds of game, and the second
number is the integer part of the expected number of dollars Alice
pays Bob.
Sample Input
3
0.6 1
1 20
0.80 8
Sample Output
1 3
20 400
24 976
I had gotten the first part of the problem, i.e the expected number of rounds of the game. I got it with the following code
if __name__ == '__main__':
t = int(raw_input())
while t :
t -= 1
temp = str(raw_input())
p,k = temp.split(' ')
p = float(p)
k = int(k)
#print p,k
ans = 0.0
num = k * (p**k)
den = 1
q = 1.0 - p
for N in range(1,k+1):
den = den - ((p**(N-1))*q)
num = num + (N*(p**(N-1))*q)
#print (N*(q**N))
print int(num/den)
But the second part of the problem is still puzzling me, i.e the expected number of dollars Alice pays bob. How can expected payoff be calculated?
You need to average all possible payouts over the probability they occur, even if you know the expected number of rounds. This means it’s more complicated than just computing the payout at the expected stop time. Here are the nitty gritty details:
Recall that the technical definition of expectation says that if X is a random variable, then the expected value of X is the sum over all possible outcomes w of X(w)*Pr(w). If X takes values in the positive integers, we can rephrase this as the expected value of X is the sum from i=1 to infinity of i*Pr(X=i). In your case, the random variables we’re dealing with are T = time the game stops, and P = payout.
The expected number rounds is the expectation of T, which is the sum from i=1 to infinity of i*Pr(T=i). Since they only ask for the integer part of the expectation, we can stop summing once i*Pr(T=i) is less than 1/2^i. (The idea on stopping the sum when i*Pr(T=i)<1/2^i is that 1/2^i sums to 1, but you might need to tweak this to avoid underestimating.)
The expectation of P is slightly more complicated. If the game were to last j rounds, then the payout would be the sum from i=1 to j of 2i-1, which turns out to be j^2. Thus only payouts of the form j^2 can occur, and Pr(P=j^2)=Pr(T=j). So the expected value of P is the sum from i=1 to infinity of i^2 * Pr(P=i^2), which is equal to the sum from i=1 to infinite of i^2*Pr(T=i). Again, we can stop summing once i^2*Pr(T=i) is less than 1/2^i.