Align Time Series with Linear Interpolation
Arrays & HashingAuto-graded · 23 test casesLast updated
Tests piecewise-linear interpolation, binary search, and boundary handling for sampling two time series at common timestamps.
You are given two time series, each represented as a list of [timestamp, value] pairs with strictly increasing timestamps. Each series defines a piecewise-linear function: between any two consecutive samples, the value changes linearly.
Your task is to sample both series at a set of query timestamps (boundaries) and return the interpolated or exact values at those points.
Rules
- If a query timestamp matches a sample timestamp exactly, return the sample's value.
- If a query timestamp falls between two consecutive samples, interpolate linearly.
- If a query timestamp is outside a series' sampled range (before the first timestamp or after the last timestamp), return
Nonefor that series. - Each output row corresponds to one boundary timestamp and contains
[timestamp, value_from_series_a, value_from_series_b].
Constraints
0 ≤ len(series_a), len(series_b) ≤ 10^41 ≤ len(boundaries) ≤ 10^4- All timestamps within a series are strictly increasing.
- Boundary timestamps are sorted in ascending order.
- Timestamp and value are floats; interpolation must use floating-point division.
Examples
Example 1
Input: sample_time_series([[0, 0], [10, 10]], [[5, 20], [15, 40]], [0, 5, 10, 15])
Output: [[0, 0, None], [5, 5, 20], [10, 10, 30], [15, None, 40]]
Example 2
Input: sample_time_series([[0, 2], [4, 6], [8, 10]], [[2, 10], [6, 18]], [1, 2, 3, 6, 8])
Output: [[1, 3, None], [2, 4, 10], [3, 5, 12], [6, 8, 18], [8, 10, None]]
Example 3
Input: sample_time_series([[1, 5], [3, 9]], [[0, 0], [4, 8]], [0, 1, 2, 3, 4])
Output: [[0, None, 0], [1, 5, 2], [2, 7, 4], [3, 9, 6], [4, None, 8]]
Hints
Hint 1
Use binary search to locate the segment bracketing each query timestamp without rescanning from the start.
Hint 2
Handle exact matches separately before attempting interpolation.
Hint 3
A query outside a series' range [first_timestamp, last_timestamp] must yield None for that series.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each time series defines a piecewise-linear function. To sample both at a common set of query timestamps, we:
- Extract timestamp lists from each series for efficient binary search.
- For each boundary timestamp, locate the relevant segment in each series using binary search:
- If the boundary matches a sample timestamp exactly, return that sample's value.
- If the boundary falls between two consecutive samples, compute the linear interpolation:
v_left + (v_right - v_left) * (t - t_left) / (t_right - t_left). - If the boundary is outside the series' range, return
None.
- Build the output row
[boundary, value_a, value_b]for each query.
This approach correctly handles edge cases: exact matches, interpolation within valid ranges, and out-of-range queries.
Why It Works
- Binary search (
bisect_left) efficiently finds the insertion point for each query timestamp, identifying the bracketing segment in logarithmic time. - Range checks ensure that queries before the first or after the last sample return
Nonerather than extrapolating incorrectly. - Exact match detection avoids floating-point interpolation errors when the query coincides with a sample.
Complexity
- Time: O((|series_a| + |series_b| + |boundaries|) + |boundaries| · (log |series_a| + log |series_b|)) = O(B · (log A + log B)) where B = len(boundaries), A = len(series_a), B = len(series_b). Extracting timestamps is linear; each boundary performs two binary searches.
- Space: O(|series_a| + |series_b| + |boundaries|) for the timestamp lists and output.
from bisect import bisect_left
from typing import List, Optional
def sample_time_series(series_a: List[List[float]], series_b: List[List[float]], boundaries: List[float]) -> List[List[Optional[float]]]:
def interpolate_at(series, timestamps, query):
if not series or query < timestamps[0] or query > timestamps[-1]:
return None
idx = bisect_left(timestamps, query)
if idx < len(timestamps) and timestamps[idx] == query:
return series[idx][1]
if idx == 0 or idx == len(series):
return None
t_left, v_left = series[idx - 1]
t_right, v_right = series[idx]
return v_left + (v_right - v_left) * ((query - t_left) / (t_right - t_left))
ts_a = [pt[0] for pt in series_a]
ts_b = [pt[0] for pt in series_b]
output = []
for boundary in boundaries:
val_a = interpolate_at(series_a, ts_a, boundary)
val_b = interpolate_at(series_b, ts_b, boundary)
output.append([boundary, val_a, val_b])
return output