Logistic Regression Diagnostics
Updated
Problem statement
You are given a feature matrix features, a binary label vector labels, and a weight vector weights. They describe a logistic regression model with no intercept term.
Let m be the number of samples. For sample i, the raw logit is z[i] = features[i] dot weights, and its probability is h[i] = sigmoid(z[i]).
Return a double[][] with exactly three rows. The rows may have different lengths:
[cost], wherecostis the mean full binary cross-entropy over all samples, using both the positive-class and the negative-class terms.- The gradient
features^T(h - labels) / m, with one value per feature column, in column order. - The sigmoid derivative
sigmoid(z[i]) * (1 - sigmoid(z[i]))for each raw logit, in sample order.
Compute each sample's loss term with this numerically stable form, where z is the sample's logit and y is its label: max(z, 0) - y * z + log(1 + exp(-abs(z))). A returned value counts as correct when it is within relative or absolute tolerance 10^-9 of the expected value.
Function
logisticRegressionDiagnostics(features: double[][], labels: int[], weights: double[]) → double[][]Examples
Example 1
features = [[1.0,0.0],[1.0,1.0]]labels = [0,1]weights = [0.0,0.0]return = [[0.6931471805599453],[0.0,-0.25],[0.25,0.25]]Both logits are 0, so both probabilities are 0.5. Each sample then has the same loss whatever its label, so the mean cost is log(2). In the gradient, the first column's residuals cancel out, and the second column only picks up the second sample's negative residual.
Example 2
features = [[1.0,-1.0],[2.0,1.0]]labels = [1,0]weights = [1.0,0.5]return = [[1.5264833592363282],[0.735371485579684,0.650841244388451],[0.2350037122015945,0.07010371654510815]]The logits are 0.5 and 2.5. The second sample has a negative label but a high probability. So its loss term is the larger one, and its residual h - y is large and positive. That pushes the cost and both gradient values up.
Example 3
features = [[1.0],[-1.0]]labels = [1,0]weights = [1000.0]return = [[0.0],[0.0],[0.0,0.0]]The logits are 1000 and -1000. The stable loss form handles them without overflow. The first sample has a positive label and a huge positive logit, and the second has a negative label and a huge negative logit. Both predictions match their labels to double precision, so every returned value comes out as zero.
Constraints
1 <= features.length <= 200.1 <= features[i].length = weights.length <= 50.labels.length = features.lengthand each label is0or1.-1000 <= features[i][j], weights[j] <= 1000.- All inputs are finite doubles, and every returned value fits in a finite double.
More LinkedIn Phone Screen questions
- Maximum Product of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Maximum Sum of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Merge Two Sorted ListsPHONE SCREEN · Seen Nov 2026
- Sqrt(x)PHONE SCREEN · Seen Nov 2026
- Total Years of Work ExperiencePHONE SCREEN · Seen Nov 2026
- Sum an Arbitrary-Dimensional ArrayPHONE SCREEN · Seen Oct 2026
- Known Words Matching Phone DigitsPHONE SCREEN · Seen Oct 2026
- Inverse-Depth Nested List SumPHONE SCREEN · Seen Oct 2026
- Valid Number with ExponentsPHONE SCREEN · Seen Oct 2026
- Sort a Quadratic-Transformed ArrayPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Column and Row Parallel Linear LayersNVIDIA · PHONE SCREENNVIDIA interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Scaled Dot-Product AttentionMeshy · OA
- Implement Circular PaddingApple · ONSITE INTERVIEWApple interview questions
- Flip Rows and Columns, Count ZerosNutanix · OANutanix interview questions
- Robot Survival ProbabilityInMobi · ONSITE INTERVIEWInMobi interview questions
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- Orthogonal Neighbor Grid TotalIMC Trading · OAIMC Trading interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN