Implement monotonic-array linear interpolation
Linear Interpolation Function
Implement a function to perform linear interpolation, calculating a y-value for a given query x-value based on a set of paired data points.
Input Parameters
You will be provided with:
array_x: A list of x-values, guaranteed to be strictly increasing (monotonic), with a lengthn >= 2.array_y: A list of corresponding y-values, of the same length asarray_x.x: A single query number, which may fall either within or outside the range defined byarray_x.
Core Tasks
-
Implement
interpolate(array_x, array_y, x): This function should return the linearly interpolated y-value whenxis within the domain[array_x[0], array_x[-1]].- If
xexactly matches anyarray_x[i], return the correspondingarray_y[i]. - Otherwise, identify the two surrounding data points
(x0, y0)and(x1, y1)such thatx0 < x < x1, and perform linear interpolation between them.
- If
-
Discuss and/or Implement Out-of-Range Behavior: Address how your function should behave when the query
xfalls outside the range[array_x[0], array_x[-1]]. Consider and be prepared to implement one or more of the following options:- Option A: Error Handling: Raise an appropriate error (e.g.,
ValueError) to indicate that extrapolation is not supported. - Option B: Clamping: Return the y-value corresponding to the nearest boundary x-value (i.e.,
array_y[0]ifx < array_x[0], orarray_y[-1]ifx > array_x[-1]). - Option C: Extrapolation: Extend the nearest segment linearly to calculate the y-value for
x.
- Option A: Error Handling: Raise an appropriate error (e.g.,
Follow-up Discussion Points
- Beyond raising an error, what other values (e.g.,
None,NaN) could be returned for out-of-range queries, and under what circumstances would each be appropriate? - Describe real-world scenarios where each of the out-of-range behaviors (Error, Clamping, Extrapolation) would be the most suitable choice, discussing their respective advantages and disadvantages.
- Given
x = 100andarray_x = [2, 4, 6, 8, 11], which out-of-range behavior would be considered the safest, and why? - Name an alternative implementation strategy for efficiently finding the surrounding points besides using binary search (e.g.,
bisectin Python).
Constraints
- Time Complexity: The target time complexity for in-range queries is O(log n).
- Edge Cases: The implementation must correctly handle edge cases such as very small input arrays (e.g.,
n=2), exact matches forx, and considerations for floating-point precision.
AI interview prep
Core dilemma
Balancing computational efficiency (O(log n)) for core interpolation with robust, context-aware handling of edge cases and diverse user requirements for out-of-range data, especially concerning data integrity and potential misinterpretation.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement monotonic-array linear interpolation
Linear Interpolation Function
Implement a function to perform linear interpolation, calculating a y-value for a given query x-value based on a set of paired data points.
Input Parameters
You will be provided with:
array_x: A list of x-values, guaranteed to be strictly increasing (monotonic), with a lengthn >= 2.array_y: A list of corresponding y-values, of the same length asarray_x.x: A single query number, which may fall either within or outside the range defined byarray_x.
Core Tasks
-
Implement
interpolate(array_x, array_y, x): This function should return the linearly interpolated y-value whenxis within the domain[array_x[0], array_x[-1]].- If
xexactly matches anyarray_x[i], return the correspondingarray_y[i]. - Otherwise, identify the two surrounding data points
(x0, y0)and(x1, y1)such thatx0 < x < x1, and perform linear interpolation between them.
- If
-
Discuss and/or Implement Out-of-Range Behavior: Address how your function should behave when the query
xfalls outside the range[array_x[0], array_x[-1]]. Consider and be prepared to implement one or more of the following options:- Option A: Error Handling: Raise an appropriate error (e.g.,
ValueError) to indicate that extrapolation is not supported. - Option B: Clamping: Return the y-value corresponding to the nearest boundary x-value (i.e.,
array_y[0]ifx < array_x[0], orarray_y[-1]ifx > array_x[-1]). - Option C: Extrapolation: Extend the nearest segment linearly to calculate the y-value for
x.
- Option A: Error Handling: Raise an appropriate error (e.g.,
Follow-up Discussion Points
- Beyond raising an error, what other values (e.g.,
None,NaN) could be returned for out-of-range queries, and under what circumstances would each be appropriate? - Describe real-world scenarios where each of the out-of-range behaviors (Error, Clamping, Extrapolation) would be the most suitable choice, discussing their respective advantages and disadvantages.
- Given
x = 100andarray_x = [2, 4, 6, 8, 11], which out-of-range behavior would be considered the safest, and why? - Name an alternative implementation strategy for efficiently finding the surrounding points besides using binary search (e.g.,
bisectin Python).
Constraints
- Time Complexity: The target time complexity for in-range queries is O(log n).
- Edge Cases: The implementation must correctly handle edge cases such as very small input arrays (e.g.,
n=2), exact matches forx, and considerations for floating-point precision.