TikTok interview questions
Big tech · 10 questions reported
- junior 1
- mid 7
- senior 2
These are 10 technical questions candidates reported being asked in TikTok interviews for AI engineer, ML engineer and applied AI engineer roles, each with a model answer you can say out loud in a minute or two. They cover attention, ml system design, multimodal, context & kv cache, reference scoring and fine-tuning & alignment: 1 junior, 7 mid-level and 2 senior questions. Every question comes from a public interview write-up that names TikTok.Updated
TikTok attention and the transformer block questions
Model answer
The candidate item is the query; the user's behaviour sequence supplies the keys and values. Each past item is embedded from its ID, category and context like time gap, then projected into a key and a value; the candidate's embedding is projected to the query.
Softmax over query-key scores weights the history, so the output is a candidate-specific interest vector, the target attention DIN introduced. Cost grows with history length per candidate, so long histories are truncated or filtered first.
Model answer
State shapes as you go: x is T by d, scores T by T. Mask before the softmax, and normalise stably.
def attention(x, Wq, Wk, Wv, mask): # x: (T, d)
q, k, v = x @ Wq, x @ Wk, x @ Wv # (T, d_k), (T, d_v)
s = q @ k.T / np.sqrt(k.shape[-1]) # (T, T)
s = np.where(mask, s, -1e9) # mask: True = keep
s = s - s.max(-1, keepdims=True)
p = np.exp(s)
p /= p.sum(-1, keepdims=True)
return p @ v # (T, d_v)
def cross_entropy(logits, y, ignore=-100): # (N, V), (N,)
keep = y != ignore
z, t = logits[keep], y[keep]
m = z.max(-1, keepdims=True)
lse = m[:, 0] + np.log(np.exp(z - m).sum(-1))
return (lse - z[np.arange(len(t)), t]).mean()
Scroll sideways to see the whole diagram
Rows are the token looking (its query), columns the token looked at (its key). Each cell is a score: the query's dot product with the key, divided by √d.
Text version
- Rows are the token looking (its query), columns the token looked at (its key). Each cell is a score: the query's dot product with the key, divided by √d.
- The causal mask: before the softmax, every score where the key comes after the query is set to minus infinity.
- Softmax turns those cells into exactly 0, and the remaining weights in each row still add up to 1.
- Token 1 sees only itself, token 2 sees tokens 1 and 2, and so on. Adding tokens later never changes an earlier row, which is why the KV cache is exact.
The causal mask as taught in lesson 3, on the attention lab's three tokens “The river bank”.
Cross-entropy is log-sum-exp minus the target logit, averaged over non-ignored tokens. Interviewers check the scale by the key width, masking scores rather than zeroing probabilities, and that padding labels are skipped.
TikTok context windows and the KV cache questions
Model answer
FlashAttention computes exact attention in tiles kept in on-chip SRAM, with an online softmax, so the full score matrix never goes to HBM. Memory becomes linear in length, and it's faster because attention is memory-bound.
Scroll sideways to see the whole diagram
Prefill runs the prompt once and computes the keys and values of all 4 prompt tokens, in every layer and every head. They go into the cache.
Text version
- Prefill runs the prompt once and computes the keys and values of all 4 prompt tokens, in every layer and every head. They go into the cache.
- Each decode step computes the key and value of the new token only, and appends them to the cache.
- The new token's query still attends to every earlier key. Those keys and values are read back from the cache, not recomputed.
- Earlier tokens' keys and values never change, because of the causal mask, so the cache is exact. With a 4-token prompt and 3 new tokens, that is 7 key-value computations with the cache and 22 without.
Counts from the KV-cache lab in lesson 5: a 4-token prompt and 3 new tokens.
The KV cache keeps past keys and values so each decode step computes only the new token, trading memory for compute. RoPE rotates queries and keys by position-dependent angles, so dot products depend on relative position, parameter-free.
TikTok scoring against references questions
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.
TikTok fine-tuning and alignment questions
Model answer
PPO-based RLHF trains a reward model, then samples and maximises reward with a KL penalty. DPO solves that objective in closed form, training on preference pairs with a classification-style loss: raise the chosen answer's log-ratio over the rejected one, relative to the reference. No reward model or sampling loop.
Scroll sideways to see the whole diagram
RLHF: fit a reward model to human preferences, then optimise the policy against it with reinforcement learning.
Text version
- RLHF: fit a reward model to human preferences, then optimise the policy against it with reinforcement learning.
- DPO: one supervised-style loss on the same pairs. No reward model, no sampling loop, far fewer moving parts.
Ouyang et al. (2022), Training language models to follow instructions with human feedback; Rafailov et al. (2023), Direct Preference Optimization.
Data: per prompt, sample several responses from the SFT model itself; humans or a validated judge pick chosen and rejected. Drop ties, watch length bias.
TikTok training and optimisation questions
Model answer
Policies are deterministic or stochastic, on- or off-policy. Policy gradients raise the log-probability of high-advantage actions. TRPO bounds each update with a KL trust region; PPO gets similar stability from a cheap clipped ratio. GAE weights TD errors exponentially, with lambda trading bias for variance.
Scroll sideways to see the whole diagram
In multi-head attention, each head projects its own queries, keys and values. Every key and value is cached for every token.
Text version
- In multi-head attention, each head projects its own queries, keys and values. Every key and value is cached for every token.
- Grouped-query attention keeps all the query heads but lets each group read one shared key-value head. Multi-query attention is the extreme: one K, V for all.
- The cache shrinks in proportion to the key-value heads, which is why most large open models use GQA.
Ainslie et al. (2023), GQA; Llama 2 70B's configuration: 64 query heads, 8 key-value heads (Touvron et al. 2023, Section 2.2).
Linear attention swaps softmax for a kernel map: linear cost, usually lower quality. GQA keeps exact attention and shares key-value heads across query groups, shrinking the KV cache.
TikTok mL system design questions
Model answer
Use LLMs where they add knowledge ID models lack, off the hot path.
Offline enrichment: generate tags, summaries and embeddings from titles, descriptions and frames. That mostly helps cold start, since a new item has meaningful features before it has clicks. Generative retrieval: give items semantic IDs, codes from quantized content embeddings, and train a sequence model to generate the next item's ID, as in TIGER. Semantic reranking: a distilled LLM rescores the top few dozen candidates against the user's intent. Judge each by offline recall and NDCG, then A/B tests.
Model answer
Predict the marginal value of more candidates per request. Label offline by replaying traffic at several K values and recording where the final ranker's top results stop changing.
Features: user activity, history length, request context and current load. A small model, say gradient-boosted trees, picks K from a few buckets to maximise value minus latency and compute cost, with a safe floor. A/B test against fixed K, watching p99 latency.
TikTok multimodal models questions
Model answer
Two encoders, image and text, project into one shared space. Train on pairs with a contrastive InfoNCE loss, as CLIP does: each image must outscore the batch with its own caption, so big batches and hard negatives matter.
Evaluate with retrieval recall at k both ways and zero-shot classification. To deploy, index item embeddings for ANN search and encode queries online. Version the index with the model: embeddings from two versions aren't comparable.
Model answer
Cut work before cutting the model. Sample a few keyframes instead of every frame, and drop near-duplicates. Use a smaller or quantized model, cap length, and batch requests to keep the GPU busy. Captions rarely need to be instant, so use an async queue and cache per video. Check quality on held-out videos.
Each question comes from a public interview write-up or video that names TikTok; the answers are ours. Not affiliated with TikTok.