---
# System prepended metadata

title: Secretary Problem

---

# Secretary Problem

We will see **N=100** job candidates whose levels are **random between 0 and 10**. We see the candidates one-by-one, and after seeing each candidate, we have 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! Our policy for selecting a candidate is the following:

* Reject the first **R** candidates, for some value **R**.
* Then hire the first one we see that is better or equal than the best among the first R.

Write a program that finds the value of R that is _maximizing the probability to hire the best out of the N candidates_ using **simulations**.

```kotlin
# Your code here

const val MAX_LEVEL = 10

/**
 * Generate a fix number of candidates that have a level between
 * 0 and MAX_LEVEL inclusive 
 * @param n The number of candidates
 * @return An random integer list containing the level of n candidates
*/
fun generateRandomCandidates(n: Int): List<Int> = 0.rangeTo(n).map(Random().nextInt(MAX_LEVEL+1))
 
fun findTheMaxOfSome(list: List<Int>, r: Int): Int {
    return 0.rangeTo(r).zip(list).map { it.second }.fold(0) { acc, currentValue ->
        acc = Int.max(acc, currentValue)
    }
}

fun hireTheNextBest(list: List<Int>, r: Int, currentMax: Int): Int {
    return list.skip(r).first { it >= currentMax }
}

fun findBestR(list: List<Int>): Int {
    return 0.rangeTo(list.size).map {
        val currentMax = findTheMaxOfSome(list, it)
        val hiredLevel = hireTheNextBest(list, it, currentMax)
        return Pair(it, hiredLevel)
    }.sort { p1, p2 -> {
        -p1.second.compareTo(p2.second)
    }}.get(0)
}

fun simulation(nbrSimulation: Int nbrCandidates: Int) {
    
    0.rangeTo(nbrSimulation).map {
        val candidates = generateRandomCandidates(nbrCandidates)
        
    }
    
}

```
