Gradient Descent for Linear Regression
Updated
Problem statement
Write gradient descent for a linear-regression model by hand. Do not use a machine-learning package or an automatic-differentiation package.
features holds n samples, and each sample has d real-valued features. targets[i] is the target for sample i. The model predicts prediction[i] = bias + sum_j(weights[j] * features[i][j]).
Loss and updates
The loss is the mean squared error L = sum_i((prediction[i] - targets[i])^2) / n. There is no factor of one half and no regularization.
- Start every feature weight and the bias at
0.0. - Do exactly
iterationsupdates. Each update looks at all samples at once (full batch). Work out every derivative of the loss from the same current weights and bias. - Then change all parameters together with
parameter = parameter - learningRate * derivative. Do not update one sample at a time or one parameter at a time. Do not shuffle samples, use momentum or stop early.
Output
Return a double[] with the d final feature weights, in the same order as the feature columns, followed by the final bias. With zero iterations or a zero learning rate, every value stays zero. Do not change the input arrays. A returned value counts as correct if its absolute or relative error is at most 10^-6.
Function
trainLinearRegression(features: double[][], targets: double[], learningRate: double, iterations: int) → double[]Examples
Example 1
features = [[1.0]]targets = [1.0]learningRate = 0.01iterations = 1return = [0.02,0.02]Everything starts at zero, so the first prediction is zero and its error (prediction minus target) is -1. The derivatives for the weight and the bias are both -2. One update sets the weight to 0.02 and the bias to 0.02, which is the answer.
Example 2
features = [[0.0],[1.0]]targets = [0.0,1.0]learningRate = 0.01iterations = 2return = [0.0198,0.0197]The first update sets the weight to 0.01 and the bias to 0.01. With these values the two errors are 0.01 and -0.98. The weight derivative is -0.98 and the bias derivative is -0.97. The second update changes both at the same time, so the answer is [0.0198,0.0197].
Example 3
features = [[1.0,-1.0],[-1.0,1.0]]targets = [1.0,-1.0]learningRate = 0.005iterations = 0return = [0.0,0.0,0.0]Zero iterations perform no updates. Return two zero feature weights followed by the zero bias: [0.0,0.0,0.0].
Constraints
1 <= n <= 200and1 <= d <= 8.features.length == targets.length == n, and every feature row has exactlydentries.- All feature and target values are finite and lie in
[-1.0, 1.0]. 0 <= learningRate <= 0.01and0 <= iterations <= 200.- Use double-precision arithmetic and implement the derivatives directly.
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Cumulative No-Intercept Regression BetasCitadel · PHONE SCREENCitadel interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Automation Pipeline, Part 2: Stage WorkersPlaid · PHONE SCREENPlaid interview questions
- Reservoir Sampling with Recorded DrawsGoogle · PHONE SCREENGoogle interview questions
- K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Token Bucket Request DecisionsDatadog · PHONE SCREENDatadog interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)