Online No-Intercept Linear Regression
Updated
Problem statement
You fit a no-intercept linear-regression model y = kx to observations that arrive in batches. The x-values are in xBatches and the matching y-values are in yBatches. Row i of each holds the observations of batch i, paired by position.
Go through the batches in order. Keep two running sums over every observation seen so far:
numerator = sum(x * y)denominator = sum(x^2)
After each full batch, the current slope is k = numerator / denominator.
Return an array with the slope after every batch, in input order. Each slope uses all observations seen so far. Update the two running sums as each batch arrives. Do not go back over observations from earlier batches.
Function
onlineNoInterceptSlopes(xBatches: double[][], yBatches: double[][]) → double[]Examples
Example 1
xBatches = [[1.0,2.0],[3.0]]yBatches = [[2.0,4.0],[9.0]]return = [2.0,2.642857142857143]After the first batch, the running numerator is 1 * 2 + 2 * 4 = 10 and the denominator is 1^2 + 2^2 = 5, so the slope is 2. The second batch adds 27 to the numerator and 9 to the denominator, giving 37 / 14.
Example 2
xBatches = [[-2.0,1.0],[0.0,4.0]]yBatches = [[4.0,1.0],[5.0,8.0]]return = [-1.4,1.1904761904761905]The first batch gives (-2 * 4 + 1 * 1) / ((-2)^2 + 1^2) = -7 / 5 = -1.4. The observation with x = 0 changes neither running sum. After adding (4, 8), the slope is 25 / 21.
Constraints
1 <= xBatches.length = yBatches.length- Every batch is non-empty, and
xBatches[i].length = yBatches[i].length. - The total number of observations across all batches is at most
10^5. - All coordinates are finite double-precision values.
- After every batch, the cumulative
sum(x^2)is positive. - Answers within an absolute or relative error of
10^-6are accepted.
More Two Sigma Online Assessment questions
- IPO Share AllocationOA · Seen Sep 2026
- Sewer Drainage PartitionOA · Seen Sep 2026
- Daily Temperature By TownOA · Seen Sep 2026
- Linear InterpolatorOA · Seen Sep 2026
- Balanced Split String with WildcardsOA · Seen Aug 2026
- Closest ColorOA · Seen Aug 2026
- Piecewise Linear Interpolation and ExtrapolationOA · Seen May 2026
- Calculate y/x using PatchOA · Seen Nov 2025
- Nums That Are Divisible by N 🍋OA · Seen Mar 2024
- Replacing Val 🍓OA · Seen Mar 2024
Similar questions at other companies
- Array Challenge (QR Intern)Akuna Capital · OAAkuna Capital interview questions
- Prefix Scores With the Running MaximumC3.ai · OAC3.ai interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- Costliest ChocolateRippling · OARippling interview questions
- Plus Mult ArrayIBM · OAIBM interview questions
- Progressive Tax CalculatorInteractive Brokers · OA
- 2020 Account Balance with Monthly Card FeesDRW · OADRW interview questions
- Range Negation UpdatesJP Morgan Chase · OAJP Morgan Chase interview questions
- Latency Bucket CounterDatadog · OA · PHONE SCREENDatadog interview questions