Minimize a black-box convex function
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's technical phone screen, last asked May 2026. It tests ternary search, golden-section search and binary-search.
You are given a black-box function F that is convex on a closed interval [a, b]. You cannot see its formula or its derivative. The only way to learn about it is to call evaluate(x), which returns F(x) for any x in [a, b]. Each call is expensive, so use as few as you can.
Implement:
def minimize(evaluate: Callable[[float], float], a: float, b: float, eps: float) -> float
Return a point x* in [a, b] with |x* - x_min| <= eps, where x_min is a point at which F attains its minimum on [a, b]. If F is constant along a whole stretch at the bottom, being within eps of any minimizer is enough.
Constraints
-
Fis convex on[a, b]. It may not be differentiable (for example|x - c|), and its minimum may be at an endpoint. -
eps > 0. The bounds may be given in either order, andamay equalb. - Report how many times your solution calls
evaluate. Expect follow-ups about an evaluation budget.
Example 1
F(x) = (x - 4)^2 + 1 on [0, 10] with eps = 0.001 → any value in [3.999, 4.001].
Example 2
F(x) = 3x + 7 on [-2, 6] with eps = 0.01 → any value in [-2, -1.99]. F is increasing, so the minimum is at the left endpoint.
Clarifying questions
- Does the call count matter more than run time? Is there a fixed budget of calls?
- Is
evaluateexact, or can it be noisy? - Should we return
x*only, or alsoF(x*)? - What should happen when
epsis smaller than floating-point precision at the given scale?
What a strong answer covers
- Swaps the bounds if
a > b, and returnsaat once whena == b. - Uses ternary search: compare
Fat two inner points and drop the third that cannot hold a minimizer. On equal values, a minimizer lies between the two points. - Improves it with golden-section search, which reuses one of the two points on each step. That needs about
1.44 x log2((b - a) / eps)calls instead of about twice as many. - Explains why convexity, not differentiability, is what makes this correct, and why it still works for
|x - c|and for a minimum at an endpoint. - Stops on the interval width, not on the change in
F, and reports the number of calls. - Tests both examples, a constant function, a function with a flat bottom, a minimum at
b, and reversed bounds.
Follow-ups
- A fixed budget of
Ncalls: Fibonacci search gives the smallest final interval. -
evaluatereturns noisy values. - The domain is integers only.
- A convex function of two variables.
Where it's asked
- Uber · Technical phone screen · Last asked May 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design
What it tests
ternary search · golden-section search · binary-search · numerical methods
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
