Level: Unknown Level
Round: Phone Screen · Type: Coding · Difficulty: 6/10 · Duration: 60 min · Interviewer: Unfriendly
Topics: Binary Search, Math, Convex Function Minimization
Interview date: 2026-01-20
I was asked a codign question like this
You are given access to a black-box function F(x) that is guaranteed to be convex over a closed interval ([a, b]). The internal definition of the function is unknown to you. The only way to interact with it is through a provided method:
evaluate(x)
which returns the value of F(x) for a given x.
Your goal is to identify a value x* within the interval ([a, b]) such that it is within a specified tolerance eps of the true minimizer of the function.
Formally, if x_min is the point where F(x) achieves its minimum, you must return an x* satisfying:
|x* - x_min| <= eps
A convex function over a closed interval is unimodal, meaning it has exactly one global minimum. This structural property enables the use of efficient search techniques that progressively narrow the search range without evaluating every point.
evaluate(x)Input:
Hidden function: (F(x) = (x - 3)^2 + 5)
Interval: ([-10, 10])
Precision: eps = 0.01
Output:
3.0
Explanation: The function reaches its minimum at (x = 3). Any value in the range ([2.99, 3.01]) is considered a valid answer.
Input:
Hidden function: (F(x) = x^2)
Interval: ([-100, 50])
Precision: eps = 0.001
Output:
0.0
Input:
Hidden function: (F(x) = |x - 123.456|)
Interval: ([0, 1000])
Precision: eps = 0.0001
Output:
123.456
My approach:
`python import math
class ConvexFunction: def init(self, func): self.func = func
def evaluate(self, x: float) -> float:
return self.func(x)
class Solution: def init(self, func: ConvexFunction): self.func = func
def minimize(self, a: float, b: float, eps: float) -> float:
while (b - a) > eps:
# Calculate two midpoints that divide the interval into three equal parts.
m1 = a + (b - a) / 3
m2 = b - (b - a) / 3
f1 = self.func.evaluate(m1)
f2 = self.func.evaluate(m2)
if f1 < f2:
b = m2
else:
a = m1
return (a + b) / 2
def test1(): print("===== Test 1 =====") func = ConvexFunction(lambda x: (x - 3)**2 + 5) solution = Solution(func) result = solution.minimize(-10, 10, 0.01) print(f"Result: {result}") # Expected: ~3.0
def test2(): print("===== Test 2 =====") func = ConvexFunction(lambda x: x**2) solution = Solution(func) result = solution.minimize(-100, 50, 0.001) print(f"Result: {result}") # Expected: ~0.0
def test3(): print("===== Test 3 =====") func = ConvexFunction(lambda x: abs(x - 123.456)) solution = Solution(func) result = solution.minimize(0, 1000, 0.0001) print(f"Result: {result}") # Expected: ~123.456
def test4(): print("===== Test 4 =====") func = ConvexFunction(lambda x: (x - 987.654)**2) solution = Solution(func) result = solution.minimize(-1e9, 1e9, 1e-4) print(f"Result: {result}") # Expected: ~987.654
def test5(): print("===== Test 5 =====") func = ConvexFunction(lambda x: math.exp(x) - 2 * x) solution = Solution(func) result = solution.minimize(0, 2, 1e-4) print(f"Result: {result}") # Expected: ~0.693
def test6(): print("===== Test 6 =====") func = ConvexFunction(lambda x: x) solution = Solution(func) result = solution.minimize(0, 100, 1e-4) print(f"Result: {result}") # Expected: ~0.0
def test7(): print("===== Test 7 =====") func = ConvexFunction(lambda x: (x - 1)**2) solution = Solution(func) result = solution.minimize(0.9999, 1.0001, 1e-4) print(f"Result: {result}") # Expected: ~1.0
if name == 'main': test1() test2() test3() test4() test5() test6() test7()
`