Whiteboard Derivations
1. Logistic Regression Gradient
Setup
Prediction:
p_i = sigmoid(z_i)
where:
z_i = x_i^T w + b
Binary cross-entropy loss:
L = -(1/n) * sum[y_i log p_i + (1 - y_i) log(1 - p_i)]
Goal
Derive:
dL/dz_i = p_i - y_igrad_w = X^T (p - y) / ngrad_b = mean(p - y)
Key Steps
- Differentiate BCE with respect to
p_i - Use derivative of sigmoid:
dp_i/dz_i = p_i (1 - p_i)
- Simplify the expression
The cancellation gives:
dL/dz_i = p_i - y_i
Then:
dL/dw = (1/n) * sum[(p_i - y_i) x_i]
which in matrix form is:
grad_w = X^T (p - y) / n
What to Say in the Interview
“The important simplification is that BCE combined with sigmoid gives a very clean derivative with respect to the logits: p - y.”
Saying it out loud (narrate while you write). “Let me set up the pieces first — I’ll write
z equals x transpose w plus b, thenp equals sigmoid of z, and underneath, the binary cross-entropy loss. I wantdL/dw, so I’ll go throughzby the chain rule:dL/dwisdL/dptimesdp/dztimesdz/dw. Taking the first factor, differentiating the loss with respect topgives meminus y over p plus one minus y over one minus p— I’ll put that over a common denominator, so it’sp minus yon top andp times one minus punderneath. Now the second factor: the sigmoid has that lovely property that its derivative isp times one minus p— I’ll write that here. And you can see those cancel exactly” — draw the strike-through — “sodL/dzis justp minus y. The last factordz/dwisx_i, so stacking over the batch gives meX transpose times p minus y, divided by n, and the bias gradient is the same thing without theX, just the mean.” Then land it: that cancellation is the whole point, and it’s why you never pair sigmoid with MSE — there thep times one minus psurvives and kills the gradient exactly where you’re most confidently wrong.
2. Softmax + Cross-Entropy
Setup
Softmax:
p_j = exp(z_j) / sum_k exp(z_k)
Cross-entropy for one-hot target y:
L = -sum_j y_j log p_j
Result
The gradient with respect to logits is:
dL/dz = p - y
Why This Matters
This is one of the most important interview derivations because it appears in almost every classification model.
What to Say
“Just like sigmoid plus BCE in binary classification, softmax plus cross-entropy gives a very clean gradient: predicted probabilities minus target distribution.”
Saying it out loud (narrate while you write). “This is the same story as the binary case, one dimension up. I’ll write the softmax, then cross-entropy against a one-hot target — and I’ll note that since
yis one-hot, the sum collapses to a single term,minus log pof the true class. Now the derivative of the softmax has two cases, so let me write them both: when I differentiatep_jwith respect to its own logit I getp_j times one minus p_j, and with respect to a different logitz_kI getminus p_j p_k. Chaining through and summing over the classes, the true-class term contributesp minus 1and every other class contributes just its ownp” — and that’s exactly the vectorp minus y. Close it: same shape of answer as sigmoid plus BCE, and it’s not a coincidence — it’s what you always get pairing an exponential-family likelihood with its natural link. Practical consequence: the gradient is bounded between minus one and one per class, which is why fused softmax-cross-entropy is both stable and fast.
3. Bernoulli MLE
Setup
If x_i are Bernoulli samples with parameter p, then:
P(x_i | p) = p^{x_i} (1 - p)^{1 - x_i}
Likelihood:
L(p) = product_i p^{x_i} (1 - p)^{1 - x_i}
Log-likelihood:
log L(p) = sum_i [x_i log p + (1 - x_i) log(1 - p)]
Differentiate
Set derivative to zero and solve:
p_hat = mean(x)
What to Say
“The MLE for a Bernoulli parameter is just the empirical fraction of ones.”
Saying it out loud (narrate while you write). “Standard MLE recipe, four moves, and I’ll say them as I go. Write the likelihood for one sample —
pto thex, timesone minus pto theone minus x, which is just a compact way of sayingpif it’s a one andone minus pif it’s a zero. Multiply over independent samples. Take logs, because the product becomes a sum and the maximizer doesn’t move” — write the log-likelihood. “Differentiate with respect top: I getthe number of ones over pminusthe number of zeros over one minus p. Set that to zero, cross-multiply” — and you land onp hat equals the sample mean. Then say why it’s satisfying: the most likely bias for the coin is exactly the fraction of heads you saw, which is what everyone would have guessed, and MLE just proved it. Worth adding the caveat: with three flips and three heads, MLE says the coin never lands tails — that’s the failure mode a prior or Laplace smoothing exists to fix.
4. Gaussian MLE
Setup
Assume x_i ~ N(mu, sigma^2).
The MLEs are:
mu_hat = sample meansigma^2_hat = (1/n) * sum (x_i - mu_hat)^2
Important Detail
This variance estimator divides by n, not n - 1.
What to Say
“For Gaussian MLE, the variance uses division by n. The unbiased estimator uses n - 1, which is a different objective.”
Saying it out loud (narrate while you write). “Same recipe as Bernoulli, two parameters instead of one. I’ll write the Gaussian log-likelihood — and immediately note the useful shape: it’s
minus n over two log sigma squared, minus the sum of squared deviations overtwo sigma squared, plus a constant I can drop. Differentiate with respect tomufirst: only the squared-error term depends on it, and setting it to zero gives the sample mean — so maximizing the likelihood is the same as minimizing squared error, which is the reason least squares and Gaussian noise are the same assumption. Now substitute that back and differentiate with respect tosigma squared” — and you land on the average squared deviation, dividing byn. Then say the part they’re fishing for: this MLE is biased downward, because you used the same data to estimatemu, and the fix isn minus 1. Two different objectives — MLE maximizes likelihood, then minus 1version is unbiased — and you cannot have both.
5. Why n - 1 for Sample Variance?
Core Intuition
Once we estimate the sample mean from the same data, one degree of freedom is used up.
If you subtract the sample mean, the centered values must sum to zero, so only n - 1 of them are free to vary independently.
Interview Answer
“The correction is there to remove the downward bias in the naive variance estimate after using the sample mean estimated from the same sample.”
Saying it out loud (narrate while you write). “Here’s the intuition without any algebra. I’ll write the
ndeviations from the sample mean — and now notice this” — writesum of (x_i minus x-bar) equals 0. “They’re forced to sum to zero. So if you tell men minus 1of them, I can compute the last one exactly; it isn’t free. That’s what a degree of freedom is, and I’ve spent one estimating the mean from the same data I’m now measuring spread on.” Then the deeper reason: the sample mean is, by construction, the point that minimizes the sum of squared deviations, so deviations from it are systematically smaller than deviations from the true mean would be. Dividing byntherefore underestimates the variance every single time, andn minus 1is exactly the correction that makes it unbiased. The number to land on: withnequal to 2 the correction is a factor of two, and bynequal to 100 it’s one percent and nobody cares.
6. Confidence Interval for a Mean
Standard Form
mean +/- critical_value * standard_error
where:
standard_error = sample_std / sqrt(n)
What to Say
“The standard error shrinks like 1/sqrt(n), which is why larger sample sizes give tighter intervals.”
Saying it out loud (narrate while you write). “Every confidence interval has the same three parts, so let me lay them out: an estimate, a measure of how noisy that estimate is, and a multiplier for how confident I want to be. The estimate is the sample mean. The noise is the standard error, which is the sample standard deviation over root
n— and the key thing to say is that this is the spread of the mean, not of the data; the data doesn’t get tighter as you collect more, your estimate of its center does. The multiplier is about 1.96 for 95 percent under a normal, or atvalue withn minus 1degrees of freedom whennis small and I’ve estimated the standard deviation.” Then the punchline: the interval shrinks like one over rootn, so cutting your error bar in half costs four times the data — that’s the number to quote. And the interpretation caveat, because interviewers probe it: the 95 percent refers to the procedure across repeated experiments, not to the probability that this particular interval contains the truth.
7. Attention Shapes
Setup
If:
Qhas shape(seq_len, d_k)Khas shape(seq_len, d_k)Vhas shape(seq_len, d_v)
then:
QK^T has shape (seq_len, seq_len)
After softmax over the key dimension:
attention_weights @ V gives shape (seq_len, d_v)
What to Say
“The attention matrix is a token-to-token relevance matrix, so its shape is sequence length by sequence length.”
Saying it out loud (narrate while you write). “I’ll do this by shapes, left to right, because shapes are where the bugs live.
Qis sequence length byd_k,Kis the same, soQtimesKtranspose is sequence by sequence” — write the box and say what it is: “that’s a token-to-token relevance matrix, rowitelling me how much tokenicares about every other token. I divide by rootd_kso the scores don’t blow up with dimension, then softmax along the row — over keys, the last axis — so each row sums to one. Multiplying byV, which is sequence byd_v, contracts the sequence dimension away and leaves me sequence byd_v: same number of tokens I started with, each one now a weighted blend of all the values.” Land on the cost: that middle matrix is the quadratic term,nsquared in both time and memory, which is the entire reason FlashAttention, sliding windows, and state-space models exist.
8. Bias-Variance Intuition
Standard Decomposition
MSE = Bias^2 + Variance + Noise
What to Say
“Bias is average systematic error. Variance is sensitivity to the training sample. More flexible models often reduce bias and increase variance.”
Saying it out loud (narrate while you write). “Let me build this up rather than quote it. Fix a test point, and imagine retraining the model on many different samples from the same distribution. Bias is the gap between the average prediction across all those models and the truth — a systematic miss that more data won’t cure, because it’s a limitation of the model class. Variance is how much those predictions scatter around their own average — that’s sensitivity to which particular sample you happened to get. And noise is the irreducible part, the floor you can’t get under no matter what.” Draw the three-term decomposition and say the crossing point: as you make the model more flexible, bias falls and variance rises, so test error traces a U and the goal is the bottom, not the left edge. Then add the honest modern footnote: for hugely over-parameterized networks the U keeps going and test error descends a second time, so the classical picture is a good intuition and an incomplete law.