Thursday, April 15, 2021

another dice game (expected value)

This is a question I often ask(ed) in interviews: 

Alice charges you $1 to play a game. She rolls a (fair) die 3 times. You win if the rolls are in strictly increasing order e.g. 1, 5, 6. You lose otherwise e.g. 1, 3, 3 or 2, 6, 1. Question is, how much should she pay you when you win, for this to be a fair bet? 

To answer this question correctly, you need to consider all cases, count them properly, and then use the formula for expected value. 

There are 216 different combinations of 3 dice - starting from (1, 1, 1) going all the way to (6, 6, 6). of these, we want to only include the ones that are strictly increasing. If we count them carefully, we find 20: (1, 2, 3); (1, 2, 4); (1, 2, 5); (1, 2, 6); (1, 3, 4); (1, 3, 5); (1, 3, 6); (1, 4, 5); (1, 4, 6); (1, 5, 6); (2, 3, 4); (2, 3, 5); (2, 3, 6); (2, 4, 5); (2, 4, 6); (2, 5, 6); (3, 4, 5); (3, 4, 6); (3, 5, 6); (4, 5, 6)

That is 20/216. So you win in 20 cases, and lose your 1$ in 216-20= 196 cases. So, to make up, you need $x such that: x*20=196*1 or x = 9.8$

The difficulty in solving this problem is not so much in the math, but in the mechanics. It takes work, most candidates are not willing to put in the effort during the interview. I'm more than happy to overlook counting errors (after all, an interview is a somewhat high pressure setting) but do expect to see how they think through the problem. 

Solving this problem in python is quite easy. Code is below to enumerate all cases and pick out the right ones. The expected value calculation is trivial once this is available.

import itertools;

x=[i for i in itertools.product(range(1,7),repeat=3)]; # all 3-dice score combinations possible

print(len(x)) # computer prints 216

y=[i for i in x if i[0]<i[1] and i[1]<i[2]]; # all 3-dice score combinations where you win

print (len(y)) # computer prints 20


No comments:

Post a Comment