Use a Fitted Line to Predict a Future Data Point
AmazonSoftware Engineer · Technical ScreenLast updated
This question tests the ability to implement a simple linear regression predictor and analyze the reliability of extrapolating time-series data using basic statistical heuristics.
Problem Statement
You are monitoring the latency of a microservice over a 24-hour window. Given a list of timestamps and corresponding latency values (in milliseconds), write a function predict_latency(data, target_timestamp) that performs simple linear regression to predict the latency at a future time.
Your function should:
- Calculate the slope (m) and intercept (b) using the ordinary least squares method.
- Return the predicted value
y = mx + bfor thetarget_timestamp. - Return
Noneif the regression is degenerate (e.g., all timestamps are identical).
Additionally, describe three quantitative indicators you would use to determine if this linear extrapolation is likely to be unreliable or misleading.
Function Signature
def predict_latency(data: list[tuple[float, float]], target_timestamp: float) -> float | None:
# data is a list of (timestamp, latency) tuples
pass
Example
Input: data = [(1.0, 10.0), (2.0, 12.0), (3.0, 14.0)], target_timestamp = 4.0
Output: 16.0
Constraints
datacontains at least two points.- Timestamps are strictly increasing.
- Latency values are non-negative.
Considerations
- What happens if the data contains high variance or significant outliers?
- How do you handle cases where the relationship is clearly non-linear (e.g., exponential growth)?
- Should you evaluate the model using a rolling window instead of all historical data?
Hints
Hint 1
Recall that the slope m is the covariance of X and Y divided by the variance of X, and the intercept b is the mean of Y minus m times the mean of X.
Hint 2
Consider calculating the R-squared value or the standard error of the residuals to quantify how well the linear model fits the existing data points.
Hint 3
Think about the impact of temporal distance: is a prediction for the next minute as reliable as a prediction for the next week given a linear model?
Follow-up questions
- How would you detect a 'regime change' where the underlying trend shifts from linear growth to a plateau?
- If you have periodic noise (e.g., daily traffic patterns), why is simple linear regression insufficient, and what feature engineering would you apply?
- How would you implement a confidence interval around your prediction to communicate uncertainty to the system operators?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.