Linear Interpolation With np.interp

Quick Overview

Implement a NumPy function that linearly interpolates sampled values at new query points with np.interp, clamping queries outside the sample range to the endpoint values. It tests knowledge of the function's defaults, output shape, complexity and the assumptions it makes about sorted sample points.

Linear Interpolation With np.interp

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `interpolate(x, xp, fp)`, which linearly interpolates a function sampled at the points `xp` with values `fp` and evaluates it at the query points `x`. The function must: - Use `np.interp(x, xp, fp)` and return an `np.ndarray` with one value per element of `x`. - Clamp a query below `xp[0]` to `fp[0]` and a query above `xp[-1]` to `fp[-1]` (no extrapolation). - Return an output with the same shape as `x`. For example: ```python xp = np.array([0.0, 1.0, 2.0]) fp = np.array([0.0, 10.0, 20.0]) x = np.array([-1.0, 0.5, 1.5, 3.0]) interpolate(x, xp, fp) # array([ 0., 5., 15., 20.]) ``` ```hint Out-of-range queries Check the defaults of the `left` and `right` arguments of `np.interp` before you write any masking code. ``` ### Constraints & Assumptions - `xp` is 1-D and strictly increasing. - `fp` is 1-D and has the same length as `xp`. - `x` can be an array of any shape. ### What a Strong Answer Covers - How queries outside `[xp[0], xp[-1]]` are handled and how that maps to the clamping requirement. - The interpolation formula for a query that falls between two samples. - How the output's shape and dtype relate to `x` and `fp`. - The time complexity of locating each query among the samples. - What `np.interp` assumes about `xp` and what happens when that assumption is violated. ### Follow-up Questions - How would you extrapolate linearly beyond the sample range instead of clamping? - What if `xp` is not sorted, or contains repeated values? - How would you interpolate samples taken at angles, where the x-axis wraps around at 360 degrees?

Overview: Implement a NumPy function that linearly interpolates sampled values at new query points with np.interp, clamping queries outside the sample range to the endpoint values. It tests knowledge of the function's defaults, output shape, complexity and the assumptions it makes about sorted sample points.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement interpolate(x, xp, fp), which linearly interpolates a function sampled at the points xp with values fp and evaluates it at the query points x.

The function must:

  • Use np.interp(x, xp, fp) and return an np.ndarray with one value per element of x .
  • Clamp a query below xp[0] to fp[0] and a query above xp[-1] to fp[-1] (no extrapolation).
  • Return an output with the same shape as x .

For example:

xp = np.array([0.0, 1.0, 2.0])
fp = np.array([0.0, 10.0, 20.0])
x  = np.array([-1.0, 0.5, 1.5, 3.0])
interpolate(x, xp, fp)
# array([ 0.,  5., 15., 20.])

Constraints & Assumptions

  • xp is 1-D and strictly increasing.
  • fp is 1-D and has the same length as xp .
  • x can be an array of any shape.

What a Strong Answer Covers Guidance

  • How queries outside [xp[0], xp[-1]] are handled and how that maps to the clamping requirement.
  • The interpolation formula for a query that falls between two samples.
  • How the output's shape and dtype relate to x and fp .
  • The time complexity of locating each query among the samples.
  • What np.interp assumes about xp and what happens when that assumption is violated.

Follow-up Questions Guidance

  • How would you extrapolate linearly beyond the sample range instead of clamping?
  • What if xp is not sorted, or contains repeated values?
  • How would you interpolate samples taken at angles, where the x-axis wraps around at 360 degrees?