Problem · Binary Search

Convex Function Minimization

Learn this problem
MediumUber logoUberFULLTIMEPHONE SCREEN
See Uber hiring insights

Problem statement

Complete the function below. The function receives the full standard input as a single string and returns the exact standard output lines.

Problem

You are given access to a black-box convex or unimodal function on a closed interval [a, b]. In this text version, the black-box function is represented as a quadratic F(x) = ax^2 + bx + c, and you must find the approximate x in the search interval that minimizes F(x).

For each query, output the minimizing x rounded to exactly six digits after the decimal point. If the unconstrained minimizer lies outside the interval, clamp it to the nearest endpoint.

This models the interview task where the only allowed operation is evaluating the black-box function; ternary search is the intended general approach.

Function

solveConvexFunctionMinimization(input: String) → String[]

Complete solveConvexFunctionMinimization. It has one parameter, String input. The first line is q. Each of the next q lines contains A B C left right for F(x)=A*x*x+B*x+C on interval [left, right]. Return one rounded minimizer per query.

Examples

Example 1

input = "3\n1 -4 7 0 10\n1 2 1 0 5\n2 -8 1 3 10"return = ["2.000000","0.000000","3.000000"]

The unconstrained minimizers are 2, -1, and 2 respectively; the second and third are clamped to their intervals.

Constraints

Each query describes a convex quadratic with A > 0.

Return answers rounded to exactly six decimal places.

More Uber problems

drafts saved locally
public String[] solveConvexFunctionMinimization(String input) {
    // write your code here
}
input"3\n1 -4 7 0 10\n1 2 1 0 5\n2 -8 1 3 10"
expected["2.000000", "0.000000", "3.000000"]
checking account