Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement monotonic x-y linear interpolation
Problem Description
Implement a function interpolate(array_x, array_y, x) that calculates and returns the interpolated y-value for a given query point x.
Inputs
array_x: A strictly increasing (monotonic) list or array of floats, with a lengthnwheren >= 2.array_y: A list or array of floats of the same length asarray_x.x: A float representing the query point. This point may be within or outside the domain defined by[array_x[0], array_x[n-1]].
Requirements
- In-Domain Interpolation: If
xfalls within the domain[array_x[0], array_x[n-1]], the function must return the piecewise-linear interpolation between the two neighboring data points. - Out-of-Domain Behavior: Discuss and optionally implement support for the following behaviors when
xis outside the domain:- Raise an error (i.e., no extrapolation).
- Clamp the result to the nearest endpoint's y-value.
- Extrapolate linearly, extending the trend of the nearest segment.
- Performance Analysis: Analyze the time complexity of your solution. Additionally, propose at least one alternative approach to using binary search (e.g.,
bisectin Python) for finding the relevant interval.
Assumptions
array_xcontains no duplicate values.- Both
array_xandarray_yare small enough to fit entirely within memory.
AI interview prep
Core dilemma
Balancing computational efficiency for finding the correct interpolation interval with robust and flexible handling of all specified edge cases, particularly out-of-range query points.
Mid-level signals3
Senior signals3
Red flags3
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement monotonic x-y linear interpolation
Problem Description
Implement a function interpolate(array_x, array_y, x) that calculates and returns the interpolated y-value for a given query point x.
Inputs
array_x: A strictly increasing (monotonic) list or array of floats, with a lengthnwheren >= 2.array_y: A list or array of floats of the same length asarray_x.x: A float representing the query point. This point may be within or outside the domain defined by[array_x[0], array_x[n-1]].
Requirements
- In-Domain Interpolation: If
xfalls within the domain[array_x[0], array_x[n-1]], the function must return the piecewise-linear interpolation between the two neighboring data points. - Out-of-Domain Behavior: Discuss and optionally implement support for the following behaviors when
xis outside the domain:- Raise an error (i.e., no extrapolation).
- Clamp the result to the nearest endpoint's y-value.
- Extrapolate linearly, extending the trend of the nearest segment.
- Performance Analysis: Analyze the time complexity of your solution. Additionally, propose at least one alternative approach to using binary search (e.g.,
bisectin Python) for finding the relevant interval.
Assumptions
array_xcontains no duplicate values.- Both
array_xandarray_yare small enough to fit entirely within memory.