FAANG Ask These 4 Python Simulations in 2021 | by Leihua Ye, PhD | Towards Data Science

Updated on Jan-10–2021
1
2
3
4
5
6
7
8
9
10
Powered by Play.ht
Create audio with Play.ht
Powered by Play.ht
Statistical Simulation is the most heavily tested topic in Data Science/Engineering Interviews! If we go over DS Interview questions posted at Glassdoor, statistical simulation is the key skill that all big tech companies expect their applicants to excel in.
Interview scenarios may either be asked to perform a power analysis of A/B experiments or construct a binomial distribution to simulate user behaviors in a programming language, R or Python.
These are not difficult questions but require a deep understanding of fundamental statistics and fluent programming skills. Without deliberate practice, these questions may likely trip you over. This post presents 4 types of the most often tested statistical distributions in Data Science interviews and live-code solutions in Python.
In a previous post, I have covered the fundamentals of statistical thinking and R codes. In case you missed it, here is the portal:
towardsdatascience.com
Disclaimer: I assume my fellow readers understand the statistical basics (e.g., what a binomial distribution is) and some familiarity with the Python programming environment (e.g., how to write a simple for loop). Here is a light refresher on common statistical distributions by Zijing Zhu.
In R or Python, please answer the following question.
For a sequence of numbers, (a1,a2,a3,a4,...,an), please write a function that randomly returns each element, ai, with probability ai/∑ai. (Condition 1)
For example, for a sequence (1,2,3,4), the function returns element 1 with a probability of 1/10 and 4 with a probability of 4/10. (Condition 2)
You can use any library, but no random.choice(). (Condition 3)
This is a real interview question that I asked by a travel company.
Let’s break it down.
The question asks for a function that returns an element proportional to its weights, ai/∑ai. It can be completed in two steps:
# Step 1: Calculate the probability for each element ai with respect to the total sum ∑ai.# Step 2: Simulate the process and return the element (it is more complicated than it sounds).
For step 1, we do something like the follow:
import numpy as npdef weight_func(sequence):# step 1    prob = []    total_sum = sum(sequence)        for i in range(len(sequence)):         prob.append(sequence[i]/total_sum) # step 2: the following is pseudo-code    return the element according to its probability
Here, there is a catch with the question: we can’t use the built-in method, random.choice() (Condition 3). Hypothetically, it would be a much easier question if we are allowed to import the Numpy package.
Alternatively, we have to develop something to perform the same functionality as the random choice.
Back then, I was clueless right on the spot. My interviewer kindly offered his first hint: you can use a uniform distribution with a range from 0 to 1 and compare the generated value (named a) to the cumulative probability sum (named cum_prob[i]) at each position i. If cum_prob[i] > a, then return the corresponding value from the sequence.
The idea sounds great, and let’s check how the Python codes look like.
People make a common mistake by trying to use a control flow (if-else statements) to filter out scenarios. It is doable for a small sample size but not practical if there are thousands of numbers in the sequence. We are not using 1,000+ “if, elif, else” statements to tell Python what to do with the numbers, right?
After looking back at this question months later, the most challenging part is to come up with the idea of using cumulative probability sum to simulate the process. It is more doable now after the detailed step-by-step explanations.
An online shopping website (e.g., Amazon, Alibaba, etc.) wants to test out two versions of banners that will appear on the website’s top. The engineering team assigns the probability of visiting version A at 0.6 and version B at 0.4.
After 10,000 visits, there are 6050 visitors being exposed to version A, and 3950 people exposed to version B.
What is the probability that there are 6050 cases when the randomization process is correct?
In other words, the probability for version A is indeed 0.6.
This is a part of a hypothesis-testing question. Let’s break it down.
There are two versions, A and B, and the experiment assigns the treatment to 10,000 people. So, it is a perfect setting for the adoption of a binomial distribution.
However, the probability of receiving version A is slightly higher than version B. The final answer should return the probability of more than 6050 people receiving version A out of 10,000 trials.
These pieces of information remind us to combine a binomial distribution with a conditional for loop, as shown below.
0.1498
The result is close to 15%, which carries practical value. This is a hypothesis testing question. Since the probability of observing 6,000 or above visitors is 15%, we fail to reject the null hypothesis and conclude there is no statistical difference between 6,000 and 6,050 visitors out of 10,000. In other words, the probability for version A is 0.6.
We are have learned hypothesis testing and how to reject or fail to reject the null hypothesis, but a question like this makes me think twice about statistical simulation.
My medium blog has 500 visits per day, and the number of visits follows a Poisson distribution. Out of 1000 times, what is the ratio of more than 510 visits per day? Write a function to simulate the process.
This is a rather straightforward question. Since the question asks for how many times an event occurs in a specified time, we can follow a Poisson procedure.
# step 1: create a poisson distribution# step 2: use an if clause to count the number
0.318
31.8% of the simulation results have more than 510 visits.
After publishing this post, the number of Medium blog skyrockets to another level.
Write a function to generate X samples from a normal distribution and plot the histogram.
This is a question asked by Google. It is a relatively easy coding question with two steps:
# step 1: generate a normal distribution# step 2: take X samples and plot the sampling distribution
Here it goes.
We generate a normal distribution with 100 numbers and set X equals to 10.
array([ 7.27305691,  6.98741057, 14.37357218, 14.17422672,  7.57495374, 10.39904815,  7.27305691,  7.41182935, 10.565957, 12.0081078 ]) # X_samples
The complete Python code is available on my Github.
As a first step, we need to understand the question before moving on to the coding part. One of the biggest mistakes that job candidates make is to jump into the programming part without asking for clarification questions. They have to re-visit the question multiple times after getting stuck.
This post focuses only on the most important types of simulations without discussing others. The underlying logic is the same: dissect the question into different steps and code each step up in Python/R.
Medium recently evolved its Writer Partner Program, which supports ordinary writers like myself. If you are not a subscriber yet and sign up via the following link, I’ll receive a portion of the membership fees.
leihua-ye.medium.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Please find me on LinkedIn and Youtube.
Also, check my other posts on Artificial Intelligence and Machine Learning.