---
# System prepended metadata

title: Untitled

---

```python=
"""
A manager wants to hire a secretary. For that he will interview candidates one-by-one and decide to hire them or not DIRECTLY after the interview

 - He will see N=100 job candidates whose levels are random between 0 and 10.
 
 - He see the candidates one-by-one, and after seeing each candidate, he has to make a decision on the spot whether to hire this candidate or not. This decision is IRREVOCABLE and we cannot change our mind later! 
 
 - He wants to maximize the probability to hire the best out of the N candidates.

Given will implement the following policy:

* Reject the first R candidates.
* Then hire the first one we see that is better than the best among the first R.

Write a program that decides the optimal threshold R using simulations. 
"""
```

```python=3.8
import numpy as np

TRIALS = 10000
QUEUE_LEN = 100

res = defaultdict(int)


def select_candidate_is_best(candidates, R):
    best_candidate = np.max(candidates)
    best_rejected = np.max(candiates[:R])

    for candidate in candidates[R:]:
        if candidate > best_rejected:
            if candidate == best_candidate:
                return 1
            else:
                return -1
    return -1

for trial in range(TRIALS):
    candidates= np.random.randint((QUEUE_LEN,), 0, 10)
    
    for R in range(100):
        res[R] += select_candidate_is_best(candidates, R)


optimal_r = list(sorted(res.items(), key=lambda tpl: tpl[1], reverse=True))[0][0]
```