This question evaluates competency in numerical interpolation and handling of piecewise linear functions, including locating the correct segment in a sorted sequence of points and managing out-of-range inputs.
You are given a polyline defined by n 2D points . Connecting consecutive points with straight line segments forms a piecewise linear function.
Given a target value x, return the corresponding function value y(x):
x
equals some
x_i
, return
y_i
.
x
lies strictly between
x_i
and
x_{i+1}
, linearly interpolate on the segment between
and
:
x < x_0
or
x > x_{n-1}
, return
null
(or a sentinel) because the function is undefined outside the polyline.
points
: list of
n
pairs
(x, y)
x
: target x-coordinate
n >= 2
x
(i.e.,
x0 < x1 < ... < x(n-1)
).
y(x)
(float), or
null
if out of range.