TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Linear Interpolation

Time SeriesData Processing
Medium

Linear interpolation is a method for filling in missing values in a sequence by drawing a straight line between the nearest known values on each side. It is one of the most common imputation techniques for time series data, preserving the local trend between observed points.

Given a Python list of numbers where some entries are None (missing), fill in the missing values using linear interpolation between the nearest known neighbors. Return a new Python list (do not use NumPy).

Algorithm

For each gap of consecutive None values between known values at positions left and right:

value[j]=vleft+j−leftright−left⋅(vright−vleft)\text{value}[j] = v_{left} + \frac{j - left}{right - left} \cdot (v_{right} - v_{left})value[j]=vleft​+right−leftj−left​⋅(vright​−vleft​)

where j is the position of the missing value, and v_left, v_right are the known values bounding the gap.

Return a list of the same length with no None values.

Loading visualization...

Examples

Input: values = [1, None, 3]

Output: [1, 2.0, 3]

Explanation: The missing midpoint is halfway between 1 and 3.

Input: values = [0, None, None, 6]

Output: [0, 2.0, 4.0, 6]

Hint 1

When a gap begins, locate the known value immediately before it and the next known value after it.

Hint 2

Fill each gap position by its fractional distance between the two endpoints.

Requirements

  • Fill each None value using linear interpolation between the nearest known values on either side
  • The first and last values are guaranteed to be non-None
  • Preserve all non-None values unchanged
  • Return a list with no None values remaining

Constraints

  • values has at least 1 element
  • First and last elements are not None
  • Values are numbers or None
  • Return a list of numbers with no None values
  • Time limit: 300 ms
Try Similar Problems
Bilinear InterpolationDifferencingSimple Moving AverageExponential Moving AverageLag Features

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Linear Interpolation

Time SeriesData Processing
Medium

Linear interpolation is a method for filling in missing values in a sequence by drawing a straight line between the nearest known values on each side. It is one of the most common imputation techniques for time series data, preserving the local trend between observed points.

Given a Python list of numbers where some entries are None (missing), fill in the missing values using linear interpolation between the nearest known neighbors. Return a new Python list (do not use NumPy).

Algorithm

For each gap of consecutive None values between known values at positions left and right:

value[j]=vleft+j−leftright−left⋅(vright−vleft)\text{value}[j] = v_{left} + \frac{j - left}{right - left} \cdot (v_{right} - v_{left})value[j]=vleft​+right−leftj−left​⋅(vright​−vleft​)

where j is the position of the missing value, and v_left, v_right are the known values bounding the gap.

Return a list of the same length with no None values.

Loading visualization...

Examples

Input: values = [1, None, 3]

Output: [1, 2.0, 3]

Explanation: The missing midpoint is halfway between 1 and 3.

Input: values = [0, None, None, 6]

Output: [0, 2.0, 4.0, 6]

Hint 1

When a gap begins, locate the known value immediately before it and the next known value after it.

Hint 2

Fill each gap position by its fractional distance between the two endpoints.

Requirements

  • Fill each None value using linear interpolation between the nearest known values on either side
  • The first and last values are guaranteed to be non-None
  • Preserve all non-None values unchanged
  • Return a list with no None values remaining

Constraints

  • values has at least 1 element
  • First and last elements are not None
  • Values are numbers or None
  • Return a list of numbers with no None values
  • Time limit: 300 ms
Try Similar Problems
Bilinear InterpolationDifferencingSimple Moving AverageExponential Moving AverageLag Features

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.