Mid-level interview questionAsked at TikTok · interview write-up
Implement AUC-ROC with correct tie handling, a numerically stable softmax, and logistic regression trained with cross-entropy and L2 regularization.
Model answer
AUC is the chance a random positive outscores a random negative, so compute it from average ranks, which counts ties as half.
def auc(y, s):
order = np.argsort(s, kind="mergesort")
ss = s[order]
ranks = np.empty(len(s))
i = 0
while i < len(s): # average rank per tie group
j = i
while j + 1 < len(s) and ss[j + 1] == ss[i]:
j += 1
ranks[order[i:j + 1]] = (i + j) / 2 + 1
i = j + 1
pos = y == 1
n_pos, n_neg = pos.sum(), (~pos).sum()
u = ranks[pos].sum() - n_pos * (n_pos + 1) / 2
return u / (n_pos * n_neg)
def softmax(z):
z = z - z.max(axis=-1, keepdims=True)
e = np.exp(z)
return e / e.sum(axis=-1, keepdims=True)
def fit_logreg(X, y, lam=0.1, lr=0.1, steps=2000):
w, b = np.zeros(X.shape[1]), 0.0
for _ in range(steps):
z = X @ w + b
p = 0.5 * (1 + np.tanh(z / 2)) # stable sigmoid
g = p - y
w -= lr * (X.T @ g / len(y) + lam * w) # no L2 on b
b -= lr * g.mean()
return w, b
Interviewers check ties, overflow on large logits, which subtracting the max prevents, and that the bias isn't regularised.
Practise the rest
8 Reference scoring questions, 492 in all, each with a model answer. Free.
More from TikTok interview questions.
More Reference scoring interview questions
- What's the difference between exact and subjective evaluation?
- What is pass@k?
- Why can BLEU or ROUGE mislead you when evaluating a model?
- When would you use semantic similarity instead of word overlap, and what does it depend on?
- How do you choose a scoring method for a new LLM feature?
- Can perplexity tell you which of two chat models is better?