Through-Origin Linear Regression
Updated
Problem statement
You are given a nonempty list of points, where points[i] = [x_i, y_i]. Here x_i is the feature and y_i is the label. Fit a line through the origin, predicted_y = w * x, by picking one real number w called the weight. Pick the weight that makes the mean squared error as small as possible:
MSE(w) = sum((y_i - w * x_i)^2) / points.length
Every feature and label is an integer. If the same point appears more than once, each copy counts as its own sample. Return the best weight as a floating-point number. Do not fit a bias or intercept.
If every feature is zero, every weight gives the same error, so return 0.0. Otherwise exactly one weight gives the smallest error. Your answer is accepted if its relative or absolute error is within 10^-9.
Function
fitThroughOrigin(points: int[][]) → doubleExamples
Example 1
points = [[1,2],[2,4],[3,6]]return = 2.0Every point lies on the line y = 2x. Each y is exactly twice its x, so this weight makes every prediction exact. The error is zero, so the answer is 2.
Example 2
points = [[1,3],[2,5],[3,7]]return = 2.4285714285714284The sum of x times y over all points is 34. The sum of x squared is 14. The best weight is the first sum divided by the second, which is 17/7. Fitting an intercept as well would solve a different problem.
Example 3
points = [[0,4],[0,-3]]return = 0.0Both x values are zero, so every weight predicts zero for both points. Every weight gives the same error, and the rule for this case says to return 0.
Constraints
1 <= points.length <= 10000.- Each point holds exactly two integers
[x_i, y_i]. -10000 <= x_i, y_i <= 10000.- Products and sums can get large, so use a wide enough number type. Every running sum has absolute value at most
10^12.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Length of a Document Reached Through a Random TokenMercor · PHONE SCREENMercor interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- One-Nearest-Neighbor Classification with Manhattan DistanceOpenAI · PHONE SCREENOpenAI interview questions
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN
- Average Grades per StudentHartford Financial Services · PHONE SCREENHartford Financial Services interview questions
- Latency Bucket CounterDatadog · OA · PHONE SCREENDatadog interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences