Mid-level interview questionAsked at Waymo · interview write-up
Implement the forward and backward passes of softmax with cross-entropy from scratch, then write a training loop for a linear classifier.
Model answer
import numpy as np
def softmax_ce(logits, y):
z = logits - logits.max(1, keepdims=True)
p = np.exp(z)
p /= p.sum(1, keepdims=True)
n = len(y)
loss = -np.log(p[np.arange(n), y] + 1e-12).mean()
grad = p.copy()
grad[np.arange(n), y] -= 1
return loss, grad / n
def train(X, y, k, lr=0.1, epochs=100):
W = np.zeros((X.shape[1], k))
b = np.zeros(k)
for _ in range(epochs):
loss, g = softmax_ce(X @ W + b, y)
W -= lr * X.T @ g
b -= lr * g.sum(0)
return W, b
Subtract the row maximum before exponentiating, for numerical stability. The gradient with respect to the logits is probabilities minus the one-hot label, averaged over the batch; the chain rule gives X transposed times that for the weights. Interviewers look for the stability trick, the averaging, and a finite-difference gradient check.
Practise the rest
35 Training questions, 492 in all, each with a model answer. Free.
More from Waymo interview questions.
More Training interview questions
- Implement masked cross-entropy in NumPy that stays numerically stable, with label smoothing and a temperature.
- Build a pipeline that finds math documents in web crawls and scores their quality.
- Explain KL divergence for language models: which direction is which, what happens when supports don't match, and how it's used as a regulariser.
- When would you use mean squared error versus cross-entropy? And what is LoRA?
- Explain Adam and its bias correction, and why AdamW decouples weight decay.
- Explain policy types in RL and modern policy-gradient methods such as TRPO, PPO and GAE, then compare linear attention and grouped-query attention.