Build a "You May Also Like" Item-to-Item Recommender
Machine LearningModel answerLast updated
Design and evaluate a simple item-to-item collaborative filtering system for an e-commerce platform recommending related products on a product detail page.
Background
You are a junior ML engineer at an online retail platform. The product team wants to add a "You May Also Like" widget on every product detail page. When a user views a pair of running shoes, the widget should show 5–10 other items they are likely to buy.
You have access to:
- Purchase history: 50 million (user, item, timestamp) transaction records over 2 years
- Item catalog: 500,000 SKUs with category, price, and title
- Page-view logs: which items each user viewed (without buying)
What is expected
Walk the interviewer through building a first version of this system end-to-end:
- Problem framing – What exactly are you predicting? What signal will you use?
- Data preparation – How do you turn raw logs into a usable dataset?
- Model choice – What algorithm would you use for v1 and why?
- Evaluation – How do you measure success offline and online?
- Failure modes – What can go wrong, and how do you mitigate it?
No need to write code. Explain your reasoning clearly, mention trade-offs, and flag common pitfalls.
Hints
Hint 1
Think about what 'similarity' means in a co-purchase context — which pairs of items appear together in the same basket or session?
Hint 2
For evaluation, consider both an offline ranking metric and an online business metric, and explain why they might disagree.
Hint 3
Cold-start is a classic problem here — how would you handle a brand-new item with zero purchase history?
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
1. Problem Framing
The goal is: given an item the user is currently viewing (the "seed" item), return a ranked list of items they are most likely to purchase next.
This is an item-to-item similarity problem, not a personalized ranking problem (we are not using who the user is — just what item they are looking at). A personalized layer can come in v2.
Positive signal: co-purchase within the same order, or co-view within the same session (weaker but higher volume). For v1, co-purchase is cleaner.
What we are not doing yet: matrix factorization of the full user-item matrix (more powerful but harder to debug and slower to iterate on).
2. Data Preparation
Step 1 – Build co-purchase pairs.
For every order containing more than one item, emit all pairs (item_a, item_b). An order with 3 items produces 3 pairs. This gives a sparse co-purchase count matrix C where C[a][b] = number of orders containing both a and b.
Step 2 – Normalize to reduce popularity bias.
Raw counts favor bestsellers. A popular item like "USB-C cable" co-occurs with everything. Use a similarity metric that penalizes popularity:
Jaccard similarity:
sim(a, b) = |users who bought both| / |users who bought a or b|
Or Lift (pointwise mutual information variant):
sim(a, b) = C[a][b] / (freq(a) * freq(b))
Lift surfaces genuinely associated pairs, not just popular ones.
Step 3 – Filter noise.
Require a minimum support threshold (e.g., at least 20 co-purchases) before including a pair. This prevents overfitting to rare coincidences.
Step 4 – Precompute the top-K neighbors.
For each item, store the top 20 most similar items. This lookup table can fit in memory or a fast key-value store (Redis).
3. Model Choice for V1
Item-to-item collaborative filtering (item-based CF) using co-purchase Jaccard similarity is the right v1 choice because:
- Simple to implement and audit (you can inspect why item B is recommended for item A)
- No user features needed — the widget works even for anonymous visitors
- Precomputed offline, so serving latency is O(1) — just a key lookup
- Degrades gracefully: if an item has few co-purchases, you fall back to category-level popularity
Why not matrix factorization or deep learning for v1? They require more engineering (training pipeline, embedding servers), are harder to debug, and the incremental quality gain over item-CF is often small on e-commerce data where the purchase signal is already strong.
4. Evaluation
Offline metric
Split data by time: train on purchases before date T, evaluate on purchases after T.
Use Recall@K: for a held-out purchase, was the bought item in the top-K recommendations shown for the seed item?
Recall@10 = fraction of test (seed → bought) pairs where bought item
appears in top-10 neighbors of seed
A reasonable baseline target for v1: Recall@10 of 5–15% (varies heavily by catalog size and data density).
Also compute coverage: what fraction of catalog items can get at least one recommendation? Items with sparse history will have low coverage — this surfaces the cold-start problem.
Online metric (A/B test)
Run a 50/50 experiment: control shows a popularity-based fallback (top-selling items in same category), treatment shows item-CF recommendations.
Primary metric: add-to-cart rate on the widget, or downstream conversion rate within the same session.
Guard-rail metric: ensure overall session revenue does not drop (sometimes users click recommendations but buy cheaper items).
Important caveat: offline Recall@K and online conversion often disagree. Offline rewards memorizing past patterns; online rewards discovery of items the user did not already know about. Do not over-optimize offline metrics.
5. Failure Modes and Mitigations
| Failure | Why it happens | Mitigation |
|---|---|---|
| Popularity bias | High-volume items dominate co-purchase counts | Normalize with Jaccard or PMI; add a diversity re-ranking step |
| Cold start (new items) | No co-purchase history yet | Fall back to content-based similarity (embed item title + category, use cosine similarity on embeddings) |
| Trivial pairs | Bundles or combo deals always bought together (phone + charger) | Filter pairs where one item is a consumable accessory; or flag them separately |
| Seasonality drift | Holiday purchasing patterns inflate certain co-purchases | Time-decay the co-purchase counts; retrain weekly |
| Position bias in evaluation | Users only buy items they see, biasing what pairs appear | Use inverse propensity scoring if you have impression logs |
Summary
For a "You May Also Like" widget, item-to-item collaborative filtering with Jaccard-normalized co-purchase counts is the correct first choice: fast to build, interpretable, and low-latency at serving time. Evaluate offline with Recall@K on a time-split, validate online with an A/B test on add-to-cart rate, and plan a v2 that layers in user personalization (e.g., weight neighbors by the viewing user's own purchase history) once the infrastructure is proven.