This question evaluates proficiency with linear interpolation on strictly increasing (monotonic) arrays, numerical reasoning about extrapolation versus clamping or error semantics, and efficient search over sorted data (performance target O(log n)).
Implement a function to interpolate a y-value for a query x-value given paired data points.
You are given:
array_x
: a
strictly increasing
list of x-values (monotonic), length
n >= 2
array_y
: list of y-values of the
same length
x
: a query number that may be
inside or outside
the range of
array_x
interpolate(array_x, array_y, x)
that returns the
linearly interpolated
y-value when
x
is within the domain.
x == array_x[i]
, return
array_y[i]
exactly.
(x0,y0)
and
(x1,y1)
such that
x0 < x < x1
and return linear interpolation.
x
is
outside
[array_x[0], array_x[-1]]
:
None
,
NaN
), and when is that appropriate?
x = 100
and
array_x = [2,4,6,8,11]
, what is the safest behavior and why?
bisect
).