Step 1 Understand it
Why interviewers ask this. It tests the two-stage pattern (retrieve, then rank), feature , feedback loops, and how to measure success.
New words in this topic (tap one to see what it means)
Watch it happen
Read the animation as text
- Scoring millions of items for every request with a heavy model is far too slow. So recommendations come in stages.
- Candidate generation: quickly gather about 1,000 items from several cheap sources — embedding similarity (approximate nearest neighbours), "bought together", popular items.
- Ranking: a richer model scores just those candidates, using user, item and context features from a feature store.
- Re-ranking applies business rules — variety, freshness, nothing already seen — and the top 10 are shown. Every impression and click is logged to train the next model, and A/B tests measure the real effect.
Requirements
Functional
- Personalised home-page recommendations.
- 'Similar items' on each item's page.
- Learn from clicks, watches and purchases.
Non-functional
- Results in about 100–200 .
- Fresh: reacts quickly to new behaviour.
- Millions of users and items.
Estimates
- 10 million users × 1 home-page load a day × 500 candidates scored ≈ 5 billion scores a day, which is why a cheap first stage must cut the candidates down.
API
GET /recommendations?user_id=…&surface=home
Data model
- Interaction (user, item, , time) → (user and item features) → .
Start simple, then scale
- Version 1: show everyone the most popular items.
- Version 2: item-to-item similarity ('people who watched X also watched Y').
- Version 3: embeddings to gather candidates, plus a ranking model.
- Version 4: a feature store, rules, exploration and .
Picture it
flowchart LR
U["Request for user 42"] --> CG["Candidate generation<br/>millions → about 1,000"]
CG --> RK["Ranking model<br/>1,000 → top 50"]
RK --> RR["Re-ranking<br/>variety and rules"] --> OUT["Home page"]
FS[("Feature store")] --> RK
LOGS[("Interaction logs")] --> TR["Offline training"] --> RKThe design, step by step
- Two stages. retrieves about 500–1,000 items quickly (embedding similarity with , items often bought together, popular items). Ranking then scores those candidates with a richer model using user, item and context features.
- Re-ranking applies business rules: variety, freshness, removing items already seen.
- Models train offline on logged interactions; features come from a feature store, defined the same way for training and serving.
- Online A/B tests measure the real impact of every change.
Deep dives
- : new users get popular and contextual items; new items use content features.
- Feedback loops and popularity bias: add some exploration.
- : one feature definition used both offline and online.
- : offline (@k, NDCG) and online (click-through, watch time, retention).
Common mistakes
- Scoring every item with a heavy model on every .
- Judging success only by offline metrics.
- Ignoring the cold-start problem.
Step 2 Go further (free, optional)
Optional. The notes above are written to be enough on their own. These free resources give a second explanation and extra practice.
-
Read
Google for Developers, free
-
Read
System Design for Recommendations and Search
Eugene Yan
-
Read
CS329S: Machine Learning Systems Design (lecture notes)
Chip Huyen, Stanford · free
Step 3 Practise
Try each exercise yourself first: on paper, out loud or in code. Then open the answer and compare it with yours.
-
ExplainSay it in plain words in under a minute, with no notes.
Explain candidate generation and ranking, and why both exist.
Show answer
There are millions of items, and only about 100–200 ms to answer. Scoring every item with an accurate model for every request would take far too long, so the work is split in two.
Candidate generation: a fast, rough first pass that cuts millions of items down to roughly 500–1,000 that might interest this user. Several cheap sources are merged: items whose embeddings are close to the user's (approximate nearest-neighbour search), items often bought or watched together with what they liked, and popular or trending items. It optimises for recall: don't miss the good ones; precision doesn't matter yet.
Ranking: a slower, accurate model scores only those candidates, using rich features about the user, the item and the context (time of day, device, recent behaviour), and predicts what you care about (click, watch time, purchase). It optimises for precision: put the best ones at the top.
Then a re-ranking step applies rules: variety, freshness, no items already seen.
Why both: the ranker is too expensive to run on everything, and the candidate generator is too rough to order the final list. Together they are both fast and good.
-
DrawSketch the diagram (or class diagram) from memory.
Draw logs → features → training → models, and request → candidates → ranking → re-ranking.
Show answer
The offline loop turns logged behaviour into features and models; the online path uses them in milliseconds. The feature store is shared by both, so a feature means the same thing in training and in serving.
textOFFLINE (learning) Apps ── clicks, watches, purchases ──► Event logs ──► feature pipelines ──► Feature store ├─ offline: history for training └─ online: latest values, fast ──► training jobs ──► Model registry ├─ embedding model ──► ANN index of item vectors └─ ranking model ONLINE (each request, ~100–200 ms) GET /recommendations?user=42 1. candidates (~500–1,000), from several sources: ANN search on the user's embedding items often bought together popular and trending 2. ranking: fetch features (online store), score each 3. re-ranking: variety, freshness, remove seen items ──► top 20 to the app └─ log what was shown ──► Event logs (closes the loop) -
ApplyUse it in a real design question and say exactly where it fits.
Build item-to-item recommendations from a small ratings dataset using in NumPy.
Show answer
Each column of the ratings matrix describes one film by who rated it and how highly. Cosine similarity between two columns is high when the same people rated both films highly. Computing it for all pairs at once is one matrix product, divided by the column lengths.
Alien's nearest film is Aliens (0.96), Toy Story's is Finding Nemo (0.94), and Heat's is Ronin (0.95): the three "tastes" in the data come out clearly. For a new user who has only rated Toy Story 5, scoring every film by its similarity to what they rated recommends Finding Nemo first.
pythonimport numpy as np films = ["Alien", "Aliens", "Toy Story", "Finding Nemo", "Heat", "Ronin"] ratings = np.array([ # rows: users, columns: films; 0 = not rated [5, 4, 0, 0, 1, 0], [4, 5, 1, 0, 0, 1], [0, 1, 5, 4, 0, 0], [1, 0, 4, 5, 0, 0], [0, 0, 0, 1, 5, 4], [1, 0, 0, 0, 4, 5], [5, 5, 0, 1, 0, 0], ]) norms = np.linalg.norm(ratings, axis=0) similarity = (ratings.T @ ratings) / np.outer(norms, norms) # film × film cosine similarity np.fill_diagonal(similarity, 0) # a film isn't its own neighbour def similar_to(film, k=2): i = films.index(film) best = np.argsort(-similarity[i])[:k] return [(films[j], round(float(similarity[i, j]), 2)) for j in best] for film in ["Alien", "Toy Story", "Heat"]: print(film, "→", similar_to(film)) # Alien → [('Aliens', 0.96), ('Finding Nemo', 0.18)] # Toy Story → [('Finding Nemo', 0.94), ('Aliens', 0.19)] # Heat → [('Ronin', 0.95), ('Alien', 0.17)] new_user = np.array([0, 0, 5, 0, 0, 0]) # only rated Toy Story scores = (similarity @ new_user).astype(float) scores[new_user > 0] = -np.inf # don't recommend what they've seen print([films[j] for j in np.argsort(-scores)[:2]]) # ['Finding Nemo', 'Aliens'] -
Trade-offsSay what it costs, when not to use it, and the alternative.
Optimising for clicks, for watch time, or for long-term retention: what can go wrong with each?
Show answer
• Clicks: easy to measure and fast to learn from, but they reward whatever makes people tap: sensational titles and misleading thumbnails. Click-through rises while satisfaction falls, and users learn to distrust the recommendations.
• Watch time: closer to real interest, and harder to fake with a thumbnail. But it favours long content over good content, and endless, compulsive viewing (autoplay rabbit holes) over what people are glad they watched; it can push towards extreme or addictive material because it holds attention.
• Long-term retention (do people come back next month?): the closest to "this product is good for people". But the signal is slow (weeks), noisy and hard to attribute to any single recommendation, so it is hard to train on directly and needs long experiments.All three, optimised blindly, can narrow what people see (filter bubbles) and favour what is already popular.
In practice, teams combine signals (clicks, watch time, likes, "not interested", surveys asking "was this worth your time?"), train for the short-term ones, and judge launches by long-term metrics plus (diversity, complaints, quality ratings), so a model that gains clicks but hurts retention doesn't ship.
Step 4 Interview-ready, then prove it
Your interview answer
About 20 to 40 seconds, said out loud. Practise it until it sounds like you, not like notes.
A two-stage system: candidate generation pulls about a thousand items cheaply using embedding similarity and simple sources, then a ranking model scores them with user, item and context features from a feature store. A final re-ranking step applies variety and business rules. Models train offline on interaction logs, and every change is checked with an online A/B test.
Follow-up questions
Why two stages?
Scoring millions of items with a heavy model on every request is too slow. A cheap retrieval step narrows the pool, so the expensive model only scores hundreds.
How do you handle a brand-new user?
Popular and trending items, context such as country and device, then adapt quickly from their first clicks.
Tick each check when you can do it without notes
A topic is finished when you pass all four checks, not when you finish a video. Be strict: if you needed your notes, it is not ticked yet.
- Explain Say it in plain words in under a minute, with no notes.
- Draw Sketch the diagram (or class diagram) from memory.
- Apply Use it in a real design question and say exactly where it fits.
- Trade-offs Say what it costs, when not to use it, and the alternative.
0 of 4 checks ticked.
Saved in this browser. Back it up or move it to another device.