Linear Regression — ML Technical Interview Questions & Answers
Predicting a continuous value with a straight-line fit.
Linear Regression
- Simple meaning: Linear regression predicts a number with a straight line. marks ≈ intercept + slope × hours. It is not for pass/fail labels.
- Picture: Plot hours vs marks. Draw the line that makes the squared errors small (ordinary least squares). Slope 8 ≈ “one extra hour, about 8 extra marks” — if the line is a fair fit.
- sklearn: LinearRegression().fit(X_train, y_train) then predict. Check RMSE or R² on test. Look at a scatter + the line — don’t trust a number only.
- Trap: using it for yes/no (use logistic). Trap: assuming the world is a straight line when the plot is clearly a curve.
Technical Interview Tip: Hours → marks, a straight line. Not pass/fail.
Code Examples for Linear Regression
Linear Regression — working example
# y ≈ 2x + 1
xs = [1, 2, 3, 4]
ys = [3, 5, 7, 9]
pred = [2 * x + 1 for x in xs]
mse = sum((y - p) ** 2 for y, p in zip(ys, pred)) / len(ys)
mae = sum(abs(y - p) for y, p in zip(ys, pred)) / len(ys)
print(pred, mse, mae)Linear Regression — regression predicts continuous values; MSE squares errors, MAE uses absolute errors.
How to Explain Linear Regression to an Interviewer
What does linear regression predict?
A number e.g. marks or price
sklearn class?
LinearRegression fit then predict