The Loop That Stalls

f is monotone: true for every x≤9x \le 9 and false for every x≥10x \ge 10. This is meant to return the largest xx with f(x) true.

def last_true(lo, hi, f):
    while lo < hi:
        mid = (lo + hi) // 2
        if f(mid):
            lo = mid
        else:
            hi = mid - 1
    return lo

last_true(1, 10, f)

What happens?

A

It overflows once lo + hi exceeds the integer range.

B

It loops forever once lo = 9 and hi = 10, because mid floors to 9 and lo is set to 9 again.

C

It returns 10, one past the threshold.

D

It returns 8, because the midpoint always rounds down.