Solving the interesting ByteDance interview question

Max NgšŸ”„
3 min readMay 29, 2020

I have read an interview question from ByteDance and found it very interesting . Here I tried to give my answer to the question.

For those who might know, ByteDance is the company publishing TikTok, the popular app in China and Worldwide.

The interview question is as follow:

10 small balls are randomly divided into 12 boxes. You need to find the probability that exactly 10 boxes are empty with a program. The program is required to simulate 100,000 times in order to calculate the probability with ā€œbrute-forceā€.

Calculating the probability with ā€œbrute-forceā€ means you have to do it with Monte Carlo method. A Monte Carlo method means you run the experiments repeatedly to obtain numerical results, which leads to the difficult part of the question:

It is not difficult for us to calculate the probability directly, which is something about 1.09e-6. The difficulty or tricky part is, to find it with simulation in 100,000 times. Because the probability is very very small, if we run the simulation for 100,000 times we usually get 0 probability, which is not the answer we want.

import random
total_number = 100000
def stimulation(balls = 10, boxes = [0] * 12):
for i in range(balls):
index = random.randint(0,11)ā€¦

--

--

Max NgšŸ”„

Original Contents of Business and AI. Your Personal Data Scientist. State-of-the-art Semi-supervised Learning. DM-> Maxnghello at gmail.com