Interview Query

Weighted Keys

3
Have you seen this question before?

Given a dictionary with weights, write a function random_key that returns a key at random with a probability proportional to the weights.

Example 1:

Input:

weights = {'A': 1, 'B': 2}

Output:

random_key(weights) -> return A 33.3%, B 66.6%

Example 2:

Input:

weights = {'A': 1, 'B': 1}

Output:

random_key(weights) -> return A 50%, B 50%
Next question: Docs Metrics
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code