FastPrepLogistic Regression with Gradient Descent
Problems/Logistic Regression with Gradient Descent

Logistic Regression with Gradient Descent

Yahoo logoYahoo● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Yahoo asked this in the phone screen for a senior machine learning engineer on the Yahoo Mail team. The candidate had to implement a two-class logistic regression model from scratch, training it with plain array operations rather than calling a machine learning library. The same screen also asked the candidate to sketch the Transformer architecture, which is not part of this problem.

The report did not give the data format, the training procedure or what to return. Here is what we chose. Use plain arrays instead of numpy. Train with full-batch gradient descent on the mean cross-entropy, starting from all-zero parameters, for a fixed number of iterations, with no regularization or early stopping. The answer is the final weights followed by the bias, accepted within 10^-6. The limits are up to 200 samples, 8 features in [-1, 1], a learning rate up to 1 and 200 iterations.

Our version is about 75% the same as the reported question.

Train a binary logistic-regression classifier with gradient descent, without a machine-learning or automatic-differentiation library. features holds n samples with d real-valued features each, and labels[i] is the class (0 or 1) of sample i.

Model. The predicted probability of class 1 is p[i] = sigmoid(bias + sum_j(weights[j] * features[i][j])), where sigmoid(z) = 1 / (1 + e^(-z)).

Training. Minimize the mean binary cross-entropy. Start with every weight and the bias at 0.0 and perform exactly iterations full-batch updates. In each update, compute the gradient from all samples with the same current parameters:

  • gradWeight[j] = sum_i((p[i] - labels[i]) * features[i][j]) / n
  • gradBias = sum_i(p[i] - labels[i]) / n

Then update every parameter at the same time with parameter = parameter - learningRate * gradient. Do not shuffle, use momentum, add regularization or stop early.

Return a double[] with the d final weights in feature order, followed by the final bias. Each value is accepted with absolute or relative error at most 10-6.

Function

trainLogisticRegression(features: double[][], labels: int[], learningRate: double, iterations: int) → double[]

Examples

Example 1

features = [[1.0],[-1.0]]labels = [1,0]learningRate = 0.5iterations = 1return = [0.25,0.0]
  • All parameters start at 0, so both predictions are sigmoid(0) = 0.5. The errors are 0.5 - 1 = -0.5 and 0.5 - 0 = 0.5.
  • Weight gradient: (-0.5 × 1 + 0.5 × (-1)) / 2 = -0.5, so the weight becomes 0 - 0.5 × (-0.5) = 0.25.
  • Bias gradient: (-0.5 + 0.5) / 2 = 0, so the bias stays 0. The answer is [0.25, 0.0].

Example 2

features = [[0.5,-0.2],[0.1,0.4],[-0.3,0.9]]labels = [1,1,0]learningRate = 0.1iterations = 0return = [0.0,0.0,0.0]
  • With zero iterations nothing is updated, so every weight and the bias stay 0.0.

Example 3

features = [[0.2,0.8],[0.9,-0.4],[-0.6,0.1],[0.3,0.3]]labels = [1,1,0,0]learningRate = 0.3iterations = 2return = [0.103720492303,0.000167276229,-0.00078737613]
  • Two full-batch updates of both weights and the bias, each computed from all four samples with the same parameter snapshot; the result is the two weights followed by the bias.

Constraints

  • 1 <= n <= 200 and 1 <= d <= 8.
  • features.length == labels.length == n, and every feature row has exactly d entries.
  • All feature values lie in [-1.0, 1.0].
  • Each labels[i] is 0 or 1.
  • 0 <= learningRate <= 1.0 and 0 <= iterations <= 200.

More Yahoo interview questions

  • Rank Unseen Items by Embedding SimilarityPHONE SCREEN · Seen Oct 2026
  • Count Binary SubstringsOA · Seen Mar 2025
  • Modify the StringOA · Seen Mar 2025

Similar questions at other companies

  • Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
  • Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
  • Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Reachable Transfer PositionsCitadel · PHONE SCREENCitadel interview questions
  • Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
  • Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
  • Filter Paginated Medical RecordsNVIDIA · PHONE SCREENNVIDIA interview questions
  • Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions
  • Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
  • Coin Game From The EndsTekion · PHONE SCREENTekion interview questions

Interview reports that mention this question

  • Yahoo Mail Senior MLE Interview Experience: 1 Experience
All Yahoo interview questionsSee Yahoo hiring insights
Saved
class Solution {
    public double[] trainLogisticRegression(double[][] features, int[] labels, double learningRate, int iterations) {
        // Return the feature weights followed by the bias.
    }
}
Checking account…