This question evaluates a candidate's understanding of piecewise linear interpolation on strictly increasing x arrays, handling of out-of-range queries (error, clamp, or extrapolate), and the design of efficient interval-search strategies.
Implement a function interpolate(array_x, array_y, x) that returns the interpolated y-value at query point x.
You are given:
array_x
: a strictly increasing (monotonic) list/array of floats (length n ≥ 2)
array_y
: a list/array of floats of the same length as
array_x
x
: a float query that may be inside or outside the range
[array_x[0], array_x[n-1]]
Requirements:
x
is within the domain, return the
piecewise-linear interpolation
between the two neighboring points.
x
is out of range:
Assume array_x has no duplicates and both arrays fit in memory.