Skip to main content

Biased Coin Flip Histogram

EasyPremium

Write a function to simulate flipping a biased coin 20 times in each of 1,000 simulations, where each flip has a 20% chance of landing heads. Then, create a histogram showing the distribution of the number of heads observed in these 1,000 simulations.

Practice this interview question in your preferred .ipynb environment. Psst... Google Colab is a free, online .ipynb environment that is easy to get up and running!

Part 1: Write the coin flipping function

Write a function to simulate the process of flipping a biased coin 20 times in each of 1,000 simulations. The coin has 20% of landing heads.

Consider what parameters you would want to include in your function to make it flexible. For instance:

  1. Number of Simulations: Should the function allow you to easily change how many times you want to run the simulation?
  2. Number of Flips per Simulation: Should you be able to specify how many times the coin is flipped in each simulation?
  3. Bias of the Coin: Should the function let you adjust the probability of landing heads (the bias of the coin)?

Think about how to design the function so that you can easily modify these parameters when needed.

Part 2: Plot the histogram

Generate a histogram to visualize the distribution of the number of heads observed in the 1,000 simulations above.