Estimate Two Conditional Win Probabilities by Simulation
ThumbtackData Scientist · Technical ScreenLast updated
This question tests practical coding ability in probabilistic simulation, requiring implementation of a Monte Carlo method to estimate conditional win probabilities under two decision strategies. It assesses a data scientist's skill in translating a classic probability scenario into working randomized code rather than relying on closed-form analysis.
Estimate Two Conditional Win Probabilities by Simulation
You are given a game with three closed doors, labeled 0, 1, and 2. Behind exactly one door is a prize (a "win"); the other two doors hide nothing. The prize is placed uniformly at random — each door is equally likely to hide it.
Each round of the game plays out as follows:
- The prize is placed uniformly at random behind one of the three doors.
- The contestant picks one of the three doors uniformly at random.
- The host, who knows where the prize is, opens one of the two remaining doors — a door that the contestant did not pick and that does not hide the prize. If both remaining doors are empty, the host chooses uniformly at random between them.
- The contestant now follows one of two fixed strategies for the entire experiment:
- Stay: keep the originally picked door.
- Switch: change to the one remaining unopened door (i.e., not the original pick and not the door the host opened).
The contestant wins the round if their final door hides the prize.
Your task is to write a program that estimates, by Monte Carlo simulation (not by an analytic/closed-form probability calculation), the two quantities:
p_stay= P(win | the contestant always stays)p_switch= P(win | the contestant always switches)
Input
A single integer n on its own line: the number of independent simulated rounds to run for each strategy.
1 <= n <= 10_000_000
Output
Print two space-separated floating-point numbers on one line: p_stay and p_switch, each the empirical win rate (number of wins divided by n) for that strategy, rounded to 2 decimal places.
For sufficiently large n, the estimates should converge to approximately 0.33 and 0.67.
Example
Input:
1000000
Output:
0.33 0.67
Requirements
- The two probabilities must be obtained from a simulation that actually plays the rounds (random prize placement, random initial pick, host opening a valid door), not by printing the known theoretical values directly.
- Use a fixed random seed so the output is reproducible: seed your random number generator with
42before running the simulation. - Each strategy is evaluated over its own
nindependent rounds. - Round each printed probability to exactly 2 decimal places (e.g.,
0.33,0.67).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.