Implement a Mini-Batch Training Loop
Updated
Problem statement
Implement a training loop for a supplied dataset and model setup.
For this exercise, assume the model is linear regression: prediction = sum(w[j]*x[i][j]) + bias. The initial array contains the d feature weights followed by the bias. Return those final parameters in the same order.
For this exercise, use mini-batch gradient descent on the mean squared error, without regularization or shuffling. In each epoch, process contiguous batches from the first sample to the last. Freeze the current weights while computing all residuals in one batch. The feature gradient is 2/b * sum(error[i]*x[i][j]) and the bias gradient is 2/b * sum(error[i]), where b is the actual batch size. Update every parameter simultaneously by subtracting rate * gradient. A final partial batch uses its own size.
For this exercise, zero epochs or zero rate leave the initial parameters unchanged. Double-precision outputs are accepted with relative-or-absolute error 10^-6.
Function
trainLinear(x: double[][], y: double[], initial: double[], rate: double, epochs: int, batchSize: int) → double[]Examples
Example 1
x = [[1]]y = [1]initial = [0,0]rate = 0.01epochs = 1batchSize = 1return = [0.02,0.02]The initial error is -1; weight and bias each increase by 0.02.
Example 2
x = [[0],[1]]y = [0,1]initial = [0,0]rate = 0.01epochs = 1batchSize = 2return = [0.01,0.01]Mean batch gradients are -1 for the weight and bias, so both become 0.01.
Example 3
x = [[1],[0],[1]]y = [1,0,1]initial = [0,0]rate = 0.01epochs = 1batchSize = 2return = [0.0296,0.0296]The first batch sets both parameters to 0.01; the final one-item batch uses its own size and frozen starting weights.
Constraints
- For this exercise, assume
1 <= n <= 200,1 <= d <= 8, and0 <= epochs <= 200. xis ann by dmatrix,yhasnentries, andinitialhasd+1entries.- Every feature, label and initial parameter is finite and lies in
[-1,1];0 <= rate <= 0.01and1 <= batchSize <= 200.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- 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
- Gradient Descent for Linear RegressionMicrosoft · PHONE SCREENMicrosoft interview questions