← Questions

Minimize a black-box convex function

0
UberCodingLast asked May 2026

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

  • F is 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, and a may equal b.
  • 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 evaluate exact, or can it be noisy?
  • Should we return x* only, or also F(x*)?
  • What should happen when eps is smaller than floating-point precision at the given scale?

What a strong answer covers

  • Swaps the bounds if a > b, and returns a at once when a == b.
  • Uses ternary search: compare F at 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 N calls: Fibonacci search gives the smallest final interval.
  • evaluate returns 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding