Back to problems

No-intercept linear regression

Algorithm · Two Sigma · Medium

Build a no-intercept linear model that maps one input feature x to a target y using a single slope parameter w. The prediction is $$\hat{y} = w x$$. Given paired observations, choose w to minimize the total squared error $$\sum_{i=1}^{n} (y_i - w x_i)^2$$. Part 1 — Batch: You are given two arrays x and y of equal length $$n$$. Return the optimal slope w as a double. Your algorithm must run in $$O(n)$$ time and use $$O(1)$$ additional space. Part 2 — Streaming: Design a data…

Checking your access…