ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def f(e, n):
    if n == 0:
        return 0
    if e == 1:
        return n
    lo, hi = 1, n
    while lo < hi:
        mid = (lo + hi) // 2
        if f(e - 1, mid - 1) < f(e, n - mid):
            lo = mid + 1
        else:
            hi = mid
    return 1 + max(f(e - 1, lo - 1), f(e, n - lo))
print(f(2, 10), f(1, 7))
  1. A. 4 8
  2. B. 5 7
  3. C. 4 7
  4. D. 3 7

Correct answer: C. 4 7

With one egg every floor must be tested, so f(1, 7) is 7. With two eggs the optimal first drop splits the floors so that the worst case is minimised, which gives 4 trials for 10 floors.

More Python Programming questions

Exams that ask this topic