Skip to content
Machine Learning & Data Science

Recommendation Systems interview questions and answers

50 questions, 41 with code. About 2 h 41 min to read them all.

By TestLab · Updated

Plan for an interview
50 questions

Track what you know and what to revise.

Sign in to track progress

You can open a few full answers without signing in. Sign in to read all

1

What is a recommendation system and how does it work?

A recommendation system is software that predicts which items (products, videos, songs, articles, people) a user is most likely to want and shows them a ranked, personalized list. It learns from past behaviour (ratings, clicks, purchases, watch time), from item attributes and from context (time, device, location), scores candidate items for each user, and returns the top N. Recommenders drive a large share of engagement on e-commerce, streaming and social platforms.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Main Approaches

Approach Idea Strength Weakness
Popularity baseline Recommend the most popular items Simple, works for new users Not personalized
Content-based Match item features to a user’s profile Handles new items Over-specializes, needs features
Collaborative filtering Use patterns across users (neighbourhoods, matrix factorization) No item features needed, finds surprising items Cold start, sparsity
Hybrid Combine signals from several methods Robust, covers cold start More complex
Deep learning Two-tower retrieval, sequence models, deep ranking Uses rich features and sequences Data and compute hungry

How It Works

  1. Collect data: the user-item matrix of interactions. Feedback is explicit (star ratings) or implicit (clicks, plays, purchases). The matrix is very sparse: most users touch a tiny fraction of items.

  2. Learn representations: similarity between items or users, or latent embeddings from matrix factorization or neural networks.

  3. Serve in two stages:

  4. Candidate generation (retrieval): pick hundreds of candidates from millions of items, often with approximate nearest-neighbour (ANN) search over embeddings.

  5. Ranking: a richer model (gradient boosting or a deep network with user, item and context features) scores the candidates.

  6. Re-ranking: apply diversity, freshness and business rules (out-of-stock, already purchased).

  7. Learn from feedback: new interactions are logged and the models are retrained.

Code Example

import numpy as np

# Rows = users, columns = items, 0 = not rated
ratings = np.array([
    [5, 4, 0, 1],
    [4, 5, 1, 0],
    [1, 0, 5, 4],
    [0, 1, 4, 5],
], dtype=float)

norms = np.linalg.norm(ratings, axis=0)
item_sim = (ratings.T @ ratings) / np.outer(norms, norms)   # item-item cosine similarity

user = 0
scores = ratings[user] @ item_sim                   # weight similar items by the user's ratings
scores[ratings[user] > 0] = -np.inf                  # don't recommend items already rated
print("recommend item:", int(np.argmax(scores)))    # item 2, the only unrated item

Evaluation

  • Offline: precision@k, recall@k, MAP, NDCG, hit rate and catalogue coverage on a time-based split (train on the past, test on the future) to avoid leakage.

  • Online: A/B tests on business metrics such as click-through, conversion, watch time and retention.

Common Pitfalls

  • Cold start: new users and items have no history; use popularity, onboarding questions and content features.

  • Popularity bias and feedback loops: the system recommends what is already popular, which then gets more clicks.

  • Filter bubbles: add diversity and exploration.

  • Privacy: behavioural data is sensitive; minimize and protect it.

Interview Tips

  • Describe the two-stage architecture; interviewers often ask how to scale to millions of items.

  • Always mention a popularity baseline to compare against.

2

What is the difference between collaborative filtering and content-based recommendations?

Content-based filtering recommends items similar to those a user liked, using item features (genre, text, tags, price), while collaborative filtering recommends items that similar users liked, using only the pattern of interactions across all users. Content-based methods work for brand-new items and are easy to explain but tend to recommend more of the same; collaborative filtering finds surprising items and needs no features but suffers when users or items have little history. Most production systems combine both in a hybrid.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x and scikit-learn 1.5+

Comparison

Aspect Content-based Collaborative filtering
Data used Item features + one user’s history User-item interactions of all users
Similarity Item features vs user profile Co-rating / co-click patterns or latent factors
New item cold start Handled (features exist) Problem (no interactions)
New user cold start Problem (no profile) Problem (no history)
Serendipity Low, over-specializes Higher, finds cross-genre items
Feature engineering Required Not required
Explainability “Because it’s a sci-fi film like X” “Users like you also watched”
Typical algorithms TF-IDF / embeddings + cosine, classifiers per user User/item k-NN, matrix factorization (SVD, ALS), neural CF

How Content-Based Works

  • Item profile: a feature vector per item (TF-IDF of descriptions, genre one-hot, text or image embeddings).

  • User profile: an aggregate (for example a rating-weighted average) of the profiles of items the user liked.

  • Score: similarity between the user profile and each unseen item.

How Collaborative Filtering Works

  • Memory-based: user-based (find similar users, recommend what they liked) or item-based (find items co-liked with the user’s items).

  • Model-based: matrix factorization learns a latent vector per user and per item; the predicted rating is their dot product.

Code Example

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Content-based: item descriptions
items = ["space opera with starships", "romantic comedy in paris",
         "starships and alien war", "paris love story"]
tfidf = TfidfVectorizer().fit_transform(items)
content_sim = cosine_similarity(tfidf)
print("content-based, similar to item 0:", int(np.argsort(-content_sim[0], kind="stable")[1]))  # item 2 (shares "starships")

# Collaborative: who interacted with what (1 = liked)
interactions = np.array([[1, 0, 1, 0],
                         [1, 0, 1, 1],
                         [0, 1, 0, 1]], dtype=float)
cf_sim = cosine_similarity(interactions.T)       # item-item similarity from users only
print("collaborative, similar to item 0:", int(np.argsort(-cf_sim[0], kind="stable")[1]))  # item 2 (liked by the same users)

When to Use Which

  • Content-based: new or fast-changing catalogues (news, jobs, classifieds), rich metadata, few users.

  • Collaborative: lots of interaction data, items whose quality isn’t captured by features (music, films).

  • Hybrid: almost always in production, for example content features feeding a collaborative model, or blending scores.

Common Pitfalls

  • Treating content-based as free of cold start: new users still need onboarding or popularity fallbacks.

  • Ignoring popularity bias in collaborative filtering, which favours already popular items.

Interview Tips

  • Give a concrete hybrid example: matrix factorization with item text embeddings as side features.

  • Be ready to explain why collaborative filtering struggles with sparsity and how matrix factorization helps.

3

What are the main challenges in building recommendation systems?

The main challenges in building recommendation systems are data sparsity, cold start for new users and items, scalability to millions of users and items with low latency, evaluating quality offline versus online, popularity bias and feedback loops, changing user preferences, and the responsibility issues of diversity, fairness and privacy. Most of these interact: sparse data makes cold start worse, and feedback loops make offline evaluation misleading, so a good design addresses them together.

Read full answer

Applies togeneral concepts (production recommender systems)

Challenges and Mitigations

Challenge Why it hurts Mitigations
Sparsity Users interact with a tiny fraction of items; similarities are noisy Matrix factorization, implicit feedback, side features
Cold start New users/items have no history Popularity and onboarding, content features, bandits for exploration
Scalability Millions of users × items; latency budgets of tens of milliseconds Two-stage retrieval + ranking, ANN search, precomputed embeddings, caching
Implicit, noisy feedback A click isn’t a like; no explicit negatives Confidence weighting, negative sampling, dwell time signals
Evaluation gap Offline metrics don’t always predict online gains Time-based splits, multiple metrics, A/B tests, interleaving
Popularity bias / feedback loops Shown items get clicks, then get shown more Exploration, debiasing (inverse propensity weighting), diversity in re-ranking
Changing preferences Tastes and trends drift; sessions differ Recency weighting, sequence models, frequent retraining
Diversity and serendipity Accurate lists can be repetitive Re-ranking (MMR), novelty objectives
Fairness Some creators or groups get little exposure Exposure constraints, auditing by group
Privacy Behaviour reveals sensitive traits Data minimization, consent, anonymization, federated or differentially private training
Business constraints Stock, margins, legal and policy rules Re-ranking rules, multi-objective optimization

Data and Engineering Challenges

  • Data quality: duplicate items, bots, inconsistent IDs and missing metadata.

  • Training-serving skew: features computed differently offline and online; use a feature store.

  • Freshness: new items and trending content need fast index updates.

  • Multi-objective trade-offs: clicks vs long-term satisfaction, user value vs revenue.

Evaluation Challenges

  • Offline: precision@k, recall@k, NDCG, MAP, hit rate and coverage, computed on a time-based split so the model doesn’t see the future.

  • Logged data only contains what the old system showed (selection bias), so offline gains may not transfer.

  • Online A/B tests measure the real business metrics but are slow and costly; short-term clicks may hurt long-term retention.

Common Pitfalls

  • Optimizing only click-through rate, which rewards clickbait.

  • Random train/test splits that leak future interactions.

  • Forgetting a popularity baseline, which is often surprisingly strong.

Interview Tips

  • Structure your answer around data, modelling, systems and responsibility.

  • Pick two or three challenges and explain concrete mitigations rather than listing everything.

4

How does the cold start problem affect recommendation systems?

The cold start problem is the lack of interaction history for new users, new items or a new system, which leaves collaborative filtering with nothing to learn from: a new user gets generic recommendations and a new item is never recommended, so it never collects the interactions it needs. It is mitigated by falling back to popularity and context, asking for preferences during onboarding, using content and metadata features, and exploring new items deliberately with bandit algorithms.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Types of Cold Start

Type Problem Example
New user No history to personalize from First visit to a shop
New item No interactions, so collaborative filtering can’t score it A newly uploaded video
New system No data at all Launching a new product

Mitigations for New Users

  • Popularity and trending lists, segmented by context (country, device, time of day, referral source).

  • Onboarding: ask for a few favourite genres, artists or topics.

  • Demographic or contextual models: use signup data or location where allowed.

  • Session-based models: personalize from the first clicks in the current session.

  • Fast updates: refresh the profile after each interaction.

Mitigations for New Items

  • Content-based features: text, image or audio embeddings and metadata place the item near similar items.

  • Hybrid models: matrix factorization or two-tower models with item feature towers can embed an item with no interactions.

  • Exploration: give new items a share of impressions with multi-armed bandits (epsilon-greedy, Thompson sampling, UCB).

  • Editorial boosts: freshness rules in re-ranking.

Code Example

import numpy as np

def recommend(user_history, popularity, item_sim, k=3):
    """Personalized scores when the user has history, popularity otherwise."""
    if len(user_history) == 0:                          # new user: cold start fallback
        return list(np.argsort(-popularity)[:k])
    scores = item_sim[user_history].sum(axis=0)          # sum similarity to items the user liked
    scores[user_history] = -np.inf                       # exclude already seen items
    return list(np.argsort(-scores)[:k])

popularity = np.array([50, 10, 30, 5, 20])
item_sim = np.eye(5)
item_sim[0, 2] = item_sim[2, 0] = 0.9
item_sim[1, 3] = item_sim[3, 1] = 0.8

print([int(i) for i in recommend([], popularity, item_sim)])    # [0, 2, 4]: most popular
print([int(i) for i in recommend([0], popularity, item_sim, k=1)])  # [2]: most similar to item 0

Measuring It

  • Report metrics separately for new users and new items; overall averages hide cold start quality.

  • Track how quickly new items receive their first interactions (time to first engagement).

Common Pitfalls

  • Letting popularity fallbacks dominate, which strengthens popularity bias.

  • Asking too many onboarding questions, which increases drop-off.

  • Training content models on features that aren’t available at serving time.

Interview Tips

  • Separate user cold start from item cold start; their fixes differ.

  • Mention exploration with bandits as the principled way to give new items a chance.

5

Why do serendipity, novelty and diversity matter in recommendations?

Serendipity, novelty and diversity are “beyond-accuracy” qualities that keep recommendations useful: diversity means the items in one list differ from each other, novelty means items the user is unlikely to know already, and serendipity means items that are both unexpected and relevant. A system tuned only for accuracy recommends obvious, popular, near-duplicate items, which bores users, narrows what they see (filter bubbles) and concentrates exposure on a few items. Balancing these qualities with relevance improves long-term satisfaction and catalogue coverage.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Definitions and Measures

Quality Meaning Common measure
Diversity Items in a list are dissimilar Intra-list diversity: average pairwise distance
Novelty Items are not already well known Mean self-information −log2(popularity share) of recommended items
Serendipity Unexpected yet relevant Relevant items not recommended by a simple baseline
Coverage Share of catalogue ever recommended Catalogue coverage, Gini coefficient of exposure

Why They Matter

  • User satisfaction: ten nearly identical items waste the list; users want options.

  • Discovery: novel items help users find things they couldn’t find themselves, which is a recommender’s value over a bestseller list.

  • Avoiding filter bubbles: diversity limits the narrowing of exposure over time.

  • Catalogue and creator health: exposure for the long tail helps new and niche creators.

  • Long-term metrics: accuracy-only lists can raise short-term clicks but reduce retention.

How to Achieve Them

  • Re-ranking with Maximal Marginal Relevance (MMR): pick each next item by relevance minus similarity to items already chosen.

  • Determinantal point processes: select sets that are both relevant and diverse.

  • Calibration: match genre proportions of the list to the user’s history.

  • Exploration: bandits give exposure to less-known items.

  • Multi-objective ranking: add novelty or diversity terms to the objective.

Code Example

import numpy as np

def mmr(relevance, similarity, k, lam=0.7):
    """Greedy Maximal Marginal Relevance re-ranking."""
    selected, candidates = [], list(range(len(relevance)))
    while candidates and len(selected) < k:
        def score(i):
            redundancy = max((similarity[i, j] for j in selected), default=0.0)
            return lam * relevance[i] - (1 - lam) * redundancy
        best = max(candidates, key=score)
        selected.append(best)
        candidates.remove(best)
    return selected

relevance = np.array([0.90, 0.88, 0.60, 0.50])
similarity = np.array([[1.0, 0.95, 0.1, 0.2],
                       [0.95, 1.0, 0.1, 0.2],
                       [0.1, 0.1, 1.0, 0.3],
                       [0.2, 0.2, 0.3, 1.0]])
print(mmr(relevance, similarity, k=2))   # [0, 2]: item 1 is a near-duplicate of item 0

Common Pitfalls

  • Adding randomness and calling it diversity: irrelevant items hurt trust.

  • Measuring only accuracy offline, so diversity changes look like losses; check them online.

  • Ignoring that the right balance differs by product (search results vs discovery feeds).

Interview Tips

  • Name the accuracy-diversity trade-off and the lam parameter that controls it.

  • Mention MMR and intra-list diversity as concrete tools.

6

How does matrix factorization work in recommendation engines?

Matrix factorization represents each user and each item as a short vector of latent factors so that the dot product of a user vector and an item vector approximates the user’s rating or preference; the vectors are learned from the observed entries of the sparse user-item matrix. Factorizing an m × n matrix into an m × k user matrix and a k × n item matrix (with k much smaller than m and n) compresses the data, fills in the missing entries as predictions, and captures hidden tastes such as “likes dark comedies” without any item features. It became famous in the Netflix Prize and is still a strong baseline.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

How It Works

  • Model: predicted rating r̂(u, i) = μ + b_u + b_i + p_u · q_i, where μ is the global mean, b_u and b_i are user and item biases, and p_u, q_i are k-dimensional factor vectors.

  • Objective: minimize squared error over observed ratings only, plus L2 regularization: Σ (r − r̂)² + λ(‖p_u‖² + ‖q_i‖² + b_u² + b_i²).

  • Training:

  • Stochastic gradient descent (SGD): loop over observed ratings and update the factors (Funk SVD).

  • Alternating Least Squares (ALS): fix item factors and solve users exactly, then swap; parallelizes well and handles implicit feedback.

  • Implicit feedback: treat all entries as preference 0/1 with confidence weights (Hu, Koren and Volinsky), usually solved with ALS.

Variants

Variant Key idea
Funk SVD / biased MF SGD on observed ratings with biases
SVD++ Adds implicit feedback of which items a user rated
ALS / weighted ALS Alternating closed-form updates; implicit-feedback version
NMF Non-negative factors, more interpretable
BPR Optimizes pairwise ranking for implicit data
Factorization machines Add side features and context

Code Example

import numpy as np

rng = np.random.default_rng(42)
R = np.array([[5, 3, 0, 1],
              [4, 0, 0, 1],
              [1, 1, 0, 5],
              [0, 1, 5, 4]], dtype=float)       # 0 = missing
users, items = np.nonzero(R)
k, lr, reg = 2, 0.01, 0.02
P = rng.normal(0, 0.1, (R.shape[0], k))          # user factors
Q = rng.normal(0, 0.1, (R.shape[1], k))          # item factors

for epoch in range(2000):
    for u, i in zip(users, items):
        err = R[u, i] - P[u] @ Q[i]              # error on an observed rating
        pu = P[u].copy()
        P[u] += lr * (err * Q[i] - reg * P[u])
        Q[i] += lr * (err * pu - reg * Q[i])

pred = P @ Q.T
print(np.round(pred, 1))                         # observed cells close to R, missing cells filled in
rmse = np.sqrt(np.mean((R[users, items] - pred[users, items]) ** 2))
print("train RMSE:", round(float(rmse), 3))

Advantages and Limitations

  • Advantages: handles sparsity well, compact, fast scoring via dot products (works with ANN retrieval), captures hidden structure.

  • Limitations: cold start for new users/items, linear interactions only, needs retraining for new data, latent factors are hard to interpret.

Common Pitfalls

  • Treating missing entries as zero ratings in explicit data (use only observed cells).

  • Too little regularization, which overfits sparse users.

  • Evaluating on random splits rather than time-based ones.

Interview Tips

  • Write the prediction formula with biases; interviewers like seeing μ + b_u + b_i.

  • Contrast SGD and ALS, and say why ALS suits implicit feedback and Spark.

7

What are user profiles and item profiles in a recommendation system?

An item profile is a representation of an item’s characteristics (genre, keywords, price, text or image embeddings, learned latent factors), and a user profile is a representation of a user’s preferences built from their interactions, stated interests and context, usually in the same feature space so the two can be compared. Recommendations come from matching them: a content-based system scores items by how close their profiles are to the user profile, and models such as matrix factorization or two-tower networks learn both profiles as embeddings whose dot product predicts preference.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Item Profiles

  • Structured attributes: category, brand, price band, release year, duration.

  • Text features: TF-IDF or sentence embeddings of titles, descriptions and reviews.

  • Media features: image or audio embeddings from pretrained networks.

  • Behavioural features: popularity, ratings, click-through rate, freshness.

  • Latent factors: item vectors learned by matrix factorization or neural models.

User Profiles

  • Explicit preferences: chosen genres, followed topics, ratings.

  • Implicit behaviour: views, clicks, purchases, dwell time, skips, weighted by recency.

  • Aggregated item features: a weighted average of the profiles of items the user liked.

  • Demographics and context (where allowed and consented): country, language, device.

  • Latent factors: user embedding from collaborative or sequence models.

How They Are Used

Use Role of profiles
Content-based filtering Cosine similarity between user and item profiles
Collaborative filtering Latent user and item vectors, dot-product scores
Ranking models Profile features are inputs to a gradient boosting or neural ranker
Cold start Item profiles let new items be recommended; onboarding builds an initial user profile
Explanations “Because you like documentaries” comes from the profile

Code Example

import numpy as np

# Item profiles: [action, comedy, documentary]
items = np.array([[1, 0, 0],
                  [1, 1, 0],
                  [0, 0, 1],
                  [0, 1, 0]], dtype=float)
liked = {0: 5, 1: 4}                              # user's ratings of items 0 and 1

# User profile: rating-weighted average of liked item profiles
user = sum(r * items[i] for i, r in liked.items()) / sum(liked.values())
scores = items @ user / (np.linalg.norm(items, axis=1) * np.linalg.norm(user))
scores[list(liked)] = -np.inf
print("user profile:", np.round(user, 2).tolist())   # [1.0, 0.44, 0.0]
print("next item:", int(np.argmax(scores)))          # item 3 (comedy) over item 2 (documentary)

Keeping Profiles Fresh

  • Decay old interactions so the profile follows changing tastes.

  • Separate long-term interests from short-term session intent.

  • Update item profiles as popularity and reviews change.

Common Pitfalls

  • Over-specialized profiles that only produce more of the same; add diversity.

  • Storing sensitive attributes without need or consent.

  • Profiles built from features that aren’t available at serving time.

Interview Tips

  • Explain that profiles must live in a comparable space (shared features or embeddings).

  • Mention negative signals (skips, low ratings) as well as positive ones.

8

What is the difference between implicit and explicit feedback in recommenders?

Explicit feedback is a preference the user states directly, such as a star rating, a like or a thumbs down, while implicit feedback is inferred from behaviour such as clicks, views, purchases, watch time, skips or searches. Explicit feedback is clear and includes negatives but is scarce, because few users rate; implicit feedback is abundant and collected automatically but is noisy and has no true negatives, because an item a user didn’t click might be unseen rather than disliked. Most production recommenders are trained mainly on implicit feedback.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Comparison

Aspect Explicit Implicit
Examples Ratings, likes, reviews Clicks, plays, purchases, dwell time, add-to-cart
Volume Low High
Negative signal Yes (low ratings, dislikes) Mostly missing; unobserved ≠ disliked
Noise Rating scale biases (some users rate high) Accidental clicks, bots, shared accounts
Matrix entries Observed ratings only Preference + confidence for all items
Typical models Rating prediction (MF with RMSE) Ranking models: weighted ALS, BPR, two-tower with negative sampling
Metrics RMSE, MAE Precision@k, recall@k, NDCG, hit rate

Working with Implicit Feedback

  • Preference and confidence (Hu, Koren and Volinsky): preference p = 1 if the user interacted, else 0; confidence c = 1 + α·count, so repeated plays count more. Weighted ALS minimizes Σ c(p − x_u·y_i)² over all user-item pairs.

  • Pairwise ranking (BPR): learn that an interacted item should score higher than a sampled non-interacted one.

  • Negative sampling: sample unobserved items as likely negatives; sample by popularity to avoid easy negatives.

  • Strength signals: watch percentage, dwell time and repeat purchases distinguish real interest from accidental clicks.

Working with Explicit Feedback

  • Centre ratings per user (subtract the user mean) to remove personal scale bias.

  • Remember ratings are missing not at random: users rate items they chose to consume, often ones they liked.

Code Example

import numpy as np

plays = np.array([[10, 0, 2],
                  [0, 3, 0]], dtype=float)       # play counts (implicit)
alpha = 4.0
preference = (plays > 0).astype(float)           # 1 if interacted
confidence = 1 + alpha * plays                   # more plays = more confident
print(preference.tolist())                       # [[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]
print(confidence.tolist())                       # [[41.0, 1.0, 9.0], [1.0, 13.0, 1.0]]

Combining Both

  • Use implicit data for scale and explicit ratings as stronger labels or as a separate objective.

  • Weight explicit negatives (dislikes, “not interested”) heavily in ranking.

Common Pitfalls

  • Treating every unobserved item as a true negative.

  • Optimizing raw clicks, which rewards clickbait; prefer completion or satisfaction signals.

  • Evaluating an implicit model with RMSE instead of ranking metrics.

Interview Tips

  • Mention the confidence weighting idea and BPR by name.

  • Explain “missing not at random” for explicit ratings.

9

What is the difference between user-based and item-based collaborative filtering?

User-based collaborative filtering recommends items liked by users whose past ratings resemble the target user’s, while item-based collaborative filtering recommends items similar to those the target user already liked, where two items are similar if the same users rated them alike. Both are neighbourhood (memory-based) methods that predict a rating as a similarity-weighted average of neighbours’ ratings. Item-based filtering is usually preferred in production because item similarities are more stable, there are often fewer items than users, and similarities can be precomputed.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

How They Work

  • User-based:

  • Compute similarity between the target user and every other user (cosine or Pearson on co-rated items).

  • Take the k most similar users who rated item i.

  • Predict r̂(u, i) = r̄_u + Σ sim(u, v)(r(v, i) − r̄_v) / Σ |sim(u, v)|.

  • Item-based:

  • Compute similarity between items from their rating columns (adjusted cosine is common).

  • For item i, take the k most similar items the user rated.

  • Predict r̂(u, i) = Σ sim(i, j) r(u, j) / Σ |sim(i, j)|.

Comparison

Aspect User-based Item-based
Neighbours Similar users Similar items
Stability User tastes change quickly Item similarities change slowly
Precomputation Harder Easy (item-item table offline)
Scale Costly with many users Better when items < users
Serendipity Higher Lower, more “similar to what you liked”
Explanation “Users like you liked” “Because you liked X”

Code Example

import numpy as np

R = np.array([[5, 4, 0, 1],
              [4, 5, 3, 1],
              [1, 1, 5, 4],
              [1, 0, 4, 5]], dtype=float)   # 0 = not rated

def cosine(a, b):
    mask = (a > 0) & (b > 0)                 # co-rated entries only
    if not mask.any():
        return 0.0
    return float(a[mask] @ b[mask] / (np.linalg.norm(a[mask]) * np.linalg.norm(b[mask])))

# User-based prediction of user 0 for item 2
sims = np.array([cosine(R[0], R[v]) for v in range(4)])
raters = [v for v in range(4) if v != 0 and R[v, 2] > 0]
user_pred = sum(sims[v] * R[v, 2] for v in raters) / sum(sims[v] for v in raters)

# Item-based prediction of user 0 for item 2
isims = np.array([cosine(R[:, 2], R[:, j]) for j in range(4)])
rated = [j for j in range(4) if R[0, j] > 0]
item_pred = sum(isims[j] * R[0, j] for j in rated) / sum(isims[j] for j in rated)

print("user-based:", round(user_pred, 2), "item-based:", round(item_pred, 2))

Common Pitfalls

  • Similarities computed from very few co-ratings; add a significance weight or shrinkage.

  • Forgetting mean-centring, so generous raters dominate.

  • Using all neighbours instead of the top k, which adds noise.

Interview Tips

  • Explain why Amazon-style “customers who bought this also bought” is item-based.

  • Mention that matrix factorization usually beats both on sparse data, but neighbourhood methods are easy to explain and update.

10

How do you implement a recommendation system with k-NN?

A k-NN recommender finds the k nearest neighbours of a user or an item in a similarity space (rating vectors, feature vectors or embeddings) and recommends items from those neighbours, scoring each candidate by a similarity-weighted average of the neighbours’ ratings. In practice you build a user-item matrix, choose a distance such as cosine, fit NearestNeighbors from scikit-learn (or an approximate nearest-neighbour index for large catalogues), and aggregate the neighbours’ interactions into a ranked top-N list.

Read full answer

Applies toPython 3.12, scikit-learn 1.5+, NumPy 2.x

Steps

  1. Build the matrix: users × items with ratings or implicit counts; keep it sparse.

  2. Normalize: mean-centre ratings per user, or use binary/confidence values for implicit data.

  3. Choose the space: user-based (rows) or item-based (columns).

  4. Fit k-NN with metric="cosine" (brute force works with sparse input).

  5. Aggregate: score unseen items by similarity-weighted ratings from the neighbours.

  6. Rank and filter: drop items already seen, apply business rules, return the top N.

  7. Evaluate with precision@k, recall@k or NDCG on a time-based split.

Code Example

import numpy as np
from scipy.sparse import csr_matrix
from sklearn.neighbors import NearestNeighbors

ratings = np.array([[5, 4, 0, 0, 1],
                    [4, 5, 4, 0, 0],
                    [0, 1, 5, 4, 0],
                    [0, 0, 4, 5, 3],
                    [5, 0, 0, 1, 4]], dtype=float)
R = csr_matrix(ratings)

knn = NearestNeighbors(n_neighbors=3, metric="cosine", algorithm="brute").fit(R)

def recommend(user, n=2):
    distances, idx = knn.kneighbors(R[user])          # the first neighbour is the user itself
    neighbours, sims = idx[0][1:], 1 - distances[0][1:]
    scores = sims @ ratings[neighbours]                # similarity-weighted ratings
    scores = scores / (sims.sum() + 1e-9)
    scores[ratings[user] > 0] = -np.inf               # remove items already rated
    return [int(i) for i in np.argsort(-scores)[:n]]

print("recommendations for user 0:", recommend(0))

Choices and Trade-offs

Choice Options Note
Similarity Cosine, Pearson, Jaccard (binary) Cosine is the usual default for sparse vectors
k 10-100 typical Small k is noisy, large k blurs tastes
Neighbourhood User-based or item-based Item-based is more stable and precomputable
Search Exact brute force or ANN (FAISS, HNSW) ANN for millions of vectors

Scaling Up

  • Precompute item-item neighbours offline and store the top k per item.

  • Use ANN indexes over embeddings for retrieval in large systems.

  • Combine k-NN candidates with a learned ranking model.

Common Pitfalls

  • Using Euclidean distance on raw ratings, where vector length dominates.

  • Dense matrices that exhaust memory; keep the data sparse.

  • Ignoring cold start: k-NN has no neighbours for new users or items.

Interview Tips

  • Explain why the first returned neighbour is the query itself.

  • Mention that libraries such as Surprise provide KNNBasic/KNNWithMeans for rating prediction.

11

Why is Alternating Least Squares (ALS) used in recommendation systems?

Alternating Least Squares (ALS) is an algorithm for learning matrix factorization models that fixes the item factors and solves for all user factors exactly with least squares, then fixes the user factors and solves for the items, repeating until convergence. Each half-step is a convex problem with a closed-form solution, so ALS converges reliably, parallelizes across users and items (which is why Spark MLlib uses it), and handles implicit feedback efficiently, where every user-item pair has a weighted preference and SGD over all pairs would be too slow.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x; Apache Spark 3.5 MLlib `ALS`

How It Works

  • Model: R ≈ U Vᵀ, with user factors U (m × k) and item factors V (n × k).

  • Loss with L2 regularization: Σ over observed (u, i) of (r_ui − u_u·v_i)² + λ(Σ‖u_u‖² + Σ‖v_i‖²).

  • The loss is not convex in U and V together, but it is convex in U with V fixed (and vice versa).

  • User step: for each user, u_u = (V_uᵀ V_u + λI)⁻¹ V_uᵀ r_u, using only the items that user rated.

  • Item step: the same with roles swapped.

  • Alternate until the loss stops decreasing (typically 10-20 iterations).

Implicit-Feedback ALS

  • Hu, Koren and Volinsky: preference p_ui = 1 if any interaction, else 0; confidence c_ui = 1 + α·count.

  • Loss over all pairs: Σ c_ui (p_ui − u_u·v_i)² + regularization.

  • A clever identity (VᵀC_uV = VᵀV + Vᵀ(C_u − I)V) makes each user update cost depend only on the items that user interacted with.

ALS vs SGD

Aspect ALS SGD
Updates Closed-form per user/item Small gradient step per rating
Parallelism Excellent (users independent given V) Harder (shared parameters)
Implicit data (all pairs) Efficient Too many pairs; needs sampling
Hyperparameters λ, k, α, iterations Also learning rate and schedule
Flexibility Squared loss models Any differentiable loss (BPR, biases, side features)

Code Example

import numpy as np

rng = np.random.default_rng(42)
R = np.array([[5, 3, 0, 1],
              [4, 0, 0, 1],
              [1, 1, 0, 5],
              [0, 1, 5, 4]], dtype=float)
mask = R > 0
k, lam = 2, 0.1
U = rng.normal(0, 0.1, (R.shape[0], k))
V = rng.normal(0, 0.1, (R.shape[1], k))

for it in range(20):
    for u in range(R.shape[0]):                     # solve each user with V fixed
        Vu = V[mask[u]]
        U[u] = np.linalg.solve(Vu.T @ Vu + lam * np.eye(k), Vu.T @ R[u, mask[u]])
    for i in range(R.shape[1]):                     # solve each item with U fixed
        Ui = U[mask[:, i]]
        V[i] = np.linalg.solve(Ui.T @ Ui + lam * np.eye(k), Ui.T @ R[mask[:, i], i])

pred = U @ V.T
rmse = np.sqrt(np.mean((R[mask] - pred[mask]) ** 2))
print("train RMSE:", round(float(rmse), 3))
print("prediction user 1, item 2:", round(float(pred[1, 2]), 2))

In Spark: ALS(rank=10, regParam=0.1, implicitPrefs=True, alpha=40.0, userCol="userId", itemCol="itemId", ratingCol="count", coldStartStrategy="drop").

Common Pitfalls

  • Forgetting regularization, which makes the solves ill-conditioned for users with few ratings.

  • Using explicit ALS on click counts; set implicitPrefs=True for implicit data.

  • Evaluating with NaN predictions for unseen users in Spark; use coldStartStrategy="drop".

Interview Tips

  • Explain why alternating makes each step convex.

  • Mention Spark MLlib and implicit feedback as the main reasons ALS is popular.

12

How is Singular Value Decomposition (SVD) used in recommendations?

Singular Value Decomposition (SVD) factorizes a matrix A into U Σ Vᵀ, where U and V have orthonormal columns and Σ holds the singular values in decreasing order; keeping only the top k singular values gives the best rank-k approximation of A, which in recommenders captures the main latent “taste” dimensions shared by users and items. Because a ratings matrix is mostly missing, classical SVD needs the gaps filled (for example with means), so practical recommenders use SVD-style matrix factorization (Funk SVD) that learns user and item factors from observed ratings only; libraries call this model “SVD” even though it is not a true SVD.

Read full answer

Applies togeneral concepts; NumPy 2.x, scikit-learn 1.5+ (`TruncatedSVD`), Surprise 1.1 (`SVD`)

Classical (Truncated) SVD

  • A (m × n) = U Σ Vᵀ; truncate to A_k = U_k Σ_k V_kᵀ (Eckart-Young: best rank-k approximation in Frobenius norm).

  • Users: rows of U_k Σ_k^½; items: rows of V_k Σ_k^½; prediction is their dot product.

  • Steps: fill missing ratings (user mean), mean-centre, compute truncated SVD, reconstruct, add the mean back.

  • Drawback: imputation biases results, and full SVD of a huge dense matrix is expensive (use sparse/randomized SVD).

Funk SVD (Matrix Factorization)

  • r̂_ui = μ + b_u + b_i + p_u · q_i, learned by SGD on observed ratings with regularization.

  • No imputation needed; handles sparsity; widely used since the Netflix Prize.

  • Extensions: SVD++ adds implicit feedback (which items the user rated).

Comparison

Aspect Classical SVD Funk SVD / MF
Missing values Must be imputed Ignored (observed only)
Factors Orthogonal Not constrained
Computation Linear algebra decomposition Iterative optimization
Biases Not modelled User and item biases
Use Dimensionality reduction, LSA, dense data Rating prediction on sparse data

Code Example

import numpy as np

R = np.array([[5, 4, 0, 1],
              [4, 5, 0, 1],
              [1, 1, 5, 4],
              [1, 0, 4, 5]], dtype=float)
mask = R > 0
user_mean = R.sum(axis=1) / mask.sum(axis=1)
filled = np.where(mask, R, user_mean[:, None])        # impute missing with user mean
centered = filled - user_mean[:, None]

U, s, Vt = np.linalg.svd(centered, full_matrices=False)
k = 2
approx = U[:, :k] @ np.diag(s[:k]) @ Vt[:k] + user_mean[:, None]
print("singular values:", np.round(s, 2).tolist())
print("predicted rating user 0, item 2:", round(float(approx[0, 2]), 2))

Choosing k

  • Look at the singular value spectrum and how much variance the top k explain.

  • Tune k on a validation set with ranking or rating metrics; too large overfits noise.

Common Pitfalls

  • Treating missing ratings as zeros, which says “the user hated it”.

  • Calling Funk SVD a true SVD in an interview without noting the difference.

  • Running dense SVD on a huge matrix instead of sparse or randomized methods.

Interview Tips

  • State Eckart-Young: truncated SVD is the optimal low-rank approximation.

  • Explain why the Netflix Prize approach learned factors from observed entries only.

13

How does association rule mining work for recommendations?

Association rule mining finds rules of the form X → Y (“customers who buy bread and butter also buy jam”) from transaction data, and a recommender uses them by suggesting Y to anyone whose basket or history contains X. Rules are kept when they have enough support (how often X and Y appear together), confidence (how often Y appears when X does) and lift (how much more often than chance). This is the classic “market basket analysis” behind “frequently bought together” sections; it isn’t personalized beyond the current basket, but it is simple, fast and explainable.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with pandas 2.x and mlxtend 0.23.4+

Key Measures

Measure Formula Meaning
Support P(X ∪ Y) = baskets with X and Y / all baskets How common the itemset is
Confidence support(X ∪ Y) / support(X) P(Y given X)
Lift confidence / support(Y) > 1 positive association, 1 independent, < 1 negative

How It Works

  1. Transactions: each basket or session is a set of items.

  2. Frequent itemsets: find itemsets with support above a threshold (Apriori, FP-Growth, Eclat).

  3. Rules: from each frequent itemset, generate rules X → Y and keep those above minimum confidence or lift.

  4. Recommend: for a user’s current basket, find rules whose antecedent X is contained in the basket and rank consequents by confidence or lift.

Code Example

import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

baskets = [["bread", "butter", "jam"],
           ["bread", "butter"],
           ["bread", "jam"],
           ["milk", "bread"],
           ["butter", "jam"]]
items = sorted({i for b in baskets for i in b})
onehot = pd.DataFrame([[i in b for i in items] for b in baskets], columns=items)

frequent = apriori(onehot, min_support=0.4, use_colnames=True)
rules = association_rules(frequent, metric="confidence", min_threshold=0.6)
print(rules[["antecedents", "consequents", "support", "confidence", "lift"]])

def recommend(basket):
    hits = rules[rules["antecedents"].apply(lambda a: a.issubset(basket))]
    hits = hits[~hits["consequents"].apply(lambda c: c.issubset(basket))]
    return hits.sort_values("lift", ascending=False)["consequents"].tolist()

print(recommend({"butter"}))

Strengths and Limitations

  • Strengths: easy to explain, needs no ratings, good for cross-selling and “frequently bought together”.

  • Limitations: not personalized beyond the basket, misses rare items (support threshold), explodes in number of rules with low thresholds, ignores order and time.

Common Pitfalls

  • Ranking by confidence only: a rule to a very popular item (milk) has high confidence but lift near 1.

  • Setting support too low, which creates millions of noisy rules.

  • Recommending items already in the basket.

Interview Tips

  • Define support, confidence and lift with a small example.

  • Mention FP-Growth as the faster alternative to Apriori, and sequential pattern mining when order matters.

14

What is a hybrid recommendation system and when should you use one?

A hybrid recommendation system combines two or more techniques, most often collaborative filtering and content-based filtering, so that each covers the other’s weaknesses: content features handle new items and sparse data, while collaborative signals add personalization and discovery. Use a hybrid when you have both interaction data and item or user features, when cold start matters, or when no single method performs well across all users; in practice almost every large production recommender is a hybrid.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Hybridization Strategies

Strategy How it works Example
Weighted Blend scores: s = w·s_cf + (1 − w)·s_cb 0.7 × collaborative + 0.3 × content
Switching Pick a method per situation Content-based for new items, CF otherwise
Mixed Show results from several methods together One row per method on a home page
Feature combination Feed content features into a CF model Factorization machines, LightFM
Feature augmentation One model’s output is a feature for another CF score as input to a GBM ranker
Cascade One method filters, another ranks Retrieval by embeddings, then a deep ranker
Meta-level One model’s learned representation feeds another Content embeddings initialize item vectors

When to Use One

  • Cold start: new items with metadata but no interactions.

  • Sparse data: CF alone overfits; features add structure.

  • Diverse user base: heavy users benefit from CF, light users from content and popularity.

  • Multiple objectives: blend relevance, freshness and diversity signals.

  • Large scale: the standard two-stage architecture (multiple retrieval sources, then a ranking model) is itself a hybrid.

Code Example

import numpy as np

cf_scores = np.array([0.9, 0.2, 0.0, 0.6])        # collaborative scores (item 2 is new: no CF signal)
cb_scores = np.array([0.5, 0.3, 0.8, 0.4])        # content similarity to the user's profile
interactions = np.array([120, 40, 0, 75])         # how much CF evidence each item has

# Weight CF more when an item has more interactions (switching blended into weighting)
w = interactions / (interactions + 50)
hybrid = w * cf_scores + (1 - w) * cb_scores
print(np.round(hybrid, 3).tolist())
print("ranking:", [int(i) for i in np.argsort(-hybrid)])

Advantages and Limitations

  • Advantages: better accuracy and coverage, handles cold start, more robust to sparse data.

  • Limitations: more components to build, tune and monitor; blending weights need validation; harder to explain.

Common Pitfalls

  • Blending scores on different scales; normalize or learn the combination with a ranker.

  • Fixed weights for all users; weights often should depend on how much data a user or item has.

  • Testing the hybrid only on warm users and missing its cold start benefit.

Interview Tips

  • Name two or three strategies (weighted, switching, feature combination) with examples.

  • Describe LightFM or factorization machines as a single-model hybrid.

15

How is deep learning used in recommendation systems?

Deep learning is used in recommendation systems to learn embeddings of users, items and context from raw and high-dimensional data, to model non-linear feature interactions in ranking, and to model the order of user behaviour with sequence models. The main patterns are two-tower retrieval models that embed users and items for fast nearest-neighbour search, deep ranking models such as Wide & Deep, DeepFM and DCN that score candidates with rich features, sequential recommenders (GRU4Rec, SASRec, BERT4Rec) that predict the next item, and graph neural networks over user-item graphs.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with PyTorch 2.x

Main Model Families

Family Examples Use
Neural collaborative filtering NCF (GMF + MLP) Learn non-linear user-item interactions
Two-tower retrieval YouTube DNN candidate generation, dual encoders Candidate generation with ANN search
Deep ranking (CTR prediction) Wide & Deep, DeepFM, DCN, DLRM Rank hundreds of candidates with many features
Sequence models GRU4Rec, SASRec, BERT4Rec Next-item and session-based recommendation
Graph neural networks PinSage, LightGCN Use the user-item graph structure
Autoencoders AutoRec, variational autoencoders for CF Reconstruct interaction vectors
Content encoders CNNs, transformers for text, image, audio Item cold start from content

Why Deep Learning Helps

  • Embeddings for huge sparse IDs (users, items, categories).

  • Non-linear interactions between many features (user, item, context, time).

  • Unstructured content: text, images and audio become features.

  • Sequences: session order and recency.

  • Multi-task learning: predict clicks, watch time and likes jointly.

Code Example

import torch
import torch.nn as nn

torch.manual_seed(42)

class TwoTower(nn.Module):
    def __init__(self, n_users, n_items, dim=32):
        super().__init__()
        self.user = nn.Sequential(nn.Embedding(n_users, dim), nn.Linear(dim, dim))
        self.item = nn.Sequential(nn.Embedding(n_items, dim), nn.Linear(dim, dim))

    def forward(self, users, items):
        u = nn.functional.normalize(self.user(users), dim=-1)
        v = nn.functional.normalize(self.item(items), dim=-1)
        return u @ v.T / 0.1                           # in-batch similarity logits (temperature 0.1)

model = TwoTower(n_users=100, n_items=500)
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
users = torch.randint(0, 100, (64,))
items = torch.randint(0, 500, (64,))                   # positive item for each user in the batch

logits = model(users, items)
loss = nn.functional.cross_entropy(logits, torch.arange(64))   # other items in the batch are negatives
loss.backward()
opt.step()
print("loss:", round(loss.item(), 3))

Common Pitfalls

  • Expecting deep models to beat well-tuned matrix factorization on small data; they often don’t.

  • In-batch negatives oversample popular items; apply a popularity (log-Q) correction.

  • Training-serving skew in features, and stale embeddings in the ANN index.

Interview Tips

  • Describe the retrieval vs ranking split and which deep models fit each stage.

  • Mention sequence models for session-based recommendation.

16

How does the Apriori algorithm work in a recommendation engine?

Apriori finds frequent itemsets in transaction data level by level: it counts single items, keeps those above a minimum support, joins them into candidate pairs, counts and prunes again, and continues to larger itemsets, using the rule that every subset of a frequent itemset must itself be frequent. The frequent itemsets are turned into association rules (X → Y) filtered by confidence and lift, and a recommendation engine suggests the consequent Y to users whose current basket contains X, as in “frequently bought together”.

Read full answer

Applies togeneral concepts; examples in Python 3.12 (standard library)

The Apriori Property

  • If an itemset is infrequent, every superset is infrequent too (support can only decrease when adding items).

  • So a candidate of size k is generated only if all its (k−1)-subsets were frequent; this pruning keeps the search manageable.

Steps

  1. L1: count each item, keep items with support ≥ min_support.

  2. Generate candidates Ck by joining frequent (k−1)-itemsets; prune any with an infrequent subset.

  3. Count each candidate’s support with a pass over the transactions; keep the frequent ones as Lk.

  4. Repeat until no new frequent itemsets.

  5. Rules: for each frequent itemset, split into X → Y and keep rules with confidence ≥ threshold; rank by lift.

  6. Recommend: match the user’s basket to rule antecedents.

Code Example

from itertools import combinations

baskets = [{"bread", "butter", "jam"}, {"bread", "butter"},
           {"bread", "jam"}, {"butter", "jam"}, {"bread", "butter", "milk"}]
min_support = 0.4
n = len(baskets)

def support(itemset):
    return sum(itemset <= b for b in baskets) / n

frequent = {}
level = [frozenset([i]) for i in set().union(*baskets)]
while level:
    level = [s for s in level if support(s) >= min_support]
    frequent.update({s: support(s) for s in level})
    size = len(level[0]) + 1 if level else 0
    candidates = {a | b for a in level for b in level if len(a | b) == size}
    level = [c for c in candidates
             if all(frozenset(sub) in frequent for sub in combinations(c, size - 1))]  # Apriori pruning

for itemset, sup in sorted(frequent.items(), key=lambda x: (-len(x[0]), -x[1])):
    print(sorted(itemset), sup)
# Includes ['bread', 'butter'] 0.6, ['bread', 'jam'] 0.4, ['butter', 'jam'] 0.4; milk (0.2) is pruned

conf = support(frozenset({"bread", "butter"})) / support(frozenset({"bread"}))
print("confidence bread -> butter:", round(conf, 2))   # 0.6 / 0.8 = 0.75

Complexity and Alternatives

Algorithm Approach Note
Apriori Breadth-first candidate generation Many passes over data
FP-Growth Compressed FP-tree, no candidate generation Usually much faster
Eclat Vertical tid-lists, intersections Fast with enough memory

Common Pitfalls

  • Too low a support threshold creates huge numbers of candidates.

  • Confusing confidence with lift; rules to popular items have high confidence but little value.

  • Applying it to long histories where order matters; use sequential pattern mining.

Interview Tips

  • State the Apriori (downward closure) property clearly; it is the heart of the algorithm.

  • Mention FP-Growth as the practical choice for large datasets.

17

What data preprocessing is needed to build a recommendation system?

Building a recommender needs preprocessing that turns raw logs and catalogues into a clean user-item interaction dataset plus features: deduplicating and filtering events, removing bots, mapping IDs to contiguous indexes, converting implicit events into weighted signals, normalizing explicit ratings, handling sparsity by filtering very rare users and items, building item and user features, and splitting the data by time. Getting these steps right often matters more than the choice of model, and mistakes such as random splits or features leaking the future make offline results misleading.

Read full answer

Applies toPython 3.12, pandas 2.x, SciPy 1.13+

Typical Steps

Step What to do
Collect and join Events (views, clicks, purchases), ratings, catalogue metadata, user attributes
Clean Remove duplicates, bots and test accounts; fix inconsistent IDs; drop deleted items
Define signals Map event types to weights (view 1, add-to-cart 3, purchase 5); use dwell time and completion
Aggregate One row per user-item with counts, last timestamp, max signal
Normalize ratings Mean-centre per user; or binarize (≥ 4 stars = positive)
Filter sparsity k-core filtering, e.g. keep users and items with ≥ 5 interactions
Encode IDs Map user and item IDs to 0..n−1 indexes for matrices and embeddings
Features Categoricals, text (TF-IDF, embeddings), numeric scaling, recency and popularity
Split Time-based (train on the past) or leave-last-out per user
Negatives For implicit data, sample non-interacted items for training and evaluation

Code Example

import pandas as pd
from scipy.sparse import csr_matrix

events = pd.DataFrame({
    "user": ["a", "a", "b", "b", "c", "a", "c"],
    "item": ["x", "y", "x", "z", "y", "x", "z"],
    "event": ["view", "purchase", "view", "view", "add_to_cart", "view", "purchase"],
    "ts": pd.to_datetime(["2026-01-01", "2026-01-02", "2026-01-03", "2026-01-04",
                          "2026-01-05", "2026-01-06", "2026-01-07"]),
}).drop_duplicates()

weights = {"view": 1, "add_to_cart": 3, "purchase": 5}
events["w"] = events["event"].map(weights)
agg = events.groupby(["user", "item"], as_index=False).agg(w=("w", "sum"), last=("ts", "max"))

agg["u"] = agg["user"].astype("category").cat.codes          # contiguous indexes
agg["i"] = agg["item"].astype("category").cat.codes

cutoff = pd.Timestamp("2026-01-05")                           # time-based split
train, test = agg[agg["last"] < cutoff], agg[agg["last"] >= cutoff]
matrix = csr_matrix((train["w"], (train["u"], train["i"])),
                    shape=(agg["u"].max() + 1, agg["i"].max() + 1))
print(matrix.toarray())
print(len(train), "train rows,", len(test), "test rows")

Common Pitfalls

  • Random splits that leak future behaviour into training.

  • Computing popularity or user features over the whole dataset, including test time.

  • Treating missing ratings as zeros.

  • Over-aggressive filtering that removes the cold start users you need to serve.

  • ID mappings that differ between training and serving.

Interview Tips

  • Emphasize event weighting and the time-based split.

  • Mention k-core filtering and its trade-off with cold start coverage.

18

How do you handle scalability and sparsity in recommendation systems?

Sparsity, where each user has interacted with only a tiny fraction of items, is handled by models that generalize across the matrix (matrix factorization and embeddings), by adding side information (content, context, social or graph signals) and by using richer implicit feedback; scalability to millions of users and items is handled by a two-stage architecture of fast candidate retrieval with approximate nearest-neighbour search and a heavier ranking model, by distributed training, sparse data structures, precomputation and caching. The two problems are linked: factorized embeddings reduce both the noise of sparse data and the cost of scoring.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with SciPy 1.13+ and NumPy 2.x

Handling Sparsity

Technique Why it helps
Matrix factorization / embeddings Shares strength across users and items through latent factors
Implicit feedback Many more signals than explicit ratings
Side information Content, metadata and context fill gaps (hybrids, factorization machines)
Graph methods Multi-hop paths connect sparse users to items
Shrinkage and regularization Avoid trusting similarities from few co-ratings
k-core filtering Remove extremely sparse rows when training (serve them with fallbacks)
Transfer learning Pretrained content embeddings and cross-domain signals

Handling Scalability

Technique Why it helps
Sparse matrices (CSR) Store only non-zero interactions
Two-stage retrieval + ranking Score only hundreds of candidates with the expensive model
ANN search (FAISS, ScaNN, HNSW) Sub-linear nearest-neighbour lookup over item embeddings
Distributed training Spark ALS, parameter servers, sharded embedding tables
Precomputation Item-item neighbours and user embeddings computed offline
Caching Popular results and features cached close to serving
Incremental updates Fold-in new users/items instead of full retraining

Code Example

import numpy as np
from scipy.sparse import random as sparse_random

rng = np.random.default_rng(42)
n_users, n_items = 10_000, 50_000
R = sparse_random(n_users, n_items, density=0.0005, format="csr", random_state=42)
print("stored values:", R.nnz, "density:", R.nnz / (n_users * n_items))   # about 250,000 values

# Embeddings from any trained model; random here for illustration
item_emb = rng.normal(size=(n_items, 32)).astype(np.float32)
item_emb /= np.linalg.norm(item_emb, axis=1, keepdims=True)
user_vec = item_emb[R[0].indices].mean(axis=0) if R[0].nnz else item_emb.mean(axis=0)

# Exact top-k with argpartition (O(n)); at larger scale an ANN index replaces this
scores = item_emb @ user_vec
top = np.argpartition(-scores, 100)[:100]
top = top[np.argsort(-scores[top])]
print("first candidates:", top[:5].tolist())

Common Pitfalls

  • Converting a sparse matrix to dense (toarray()) on real data.

  • Computing full user × item score matrices instead of retrieving candidates.

  • Stale ANN indexes that miss new items.

Interview Tips

  • Give rough numbers: retrieval narrows millions to hundreds within tens of milliseconds.

  • Explain how embeddings solve sparsity and enable ANN at the same time.

19

What are the data privacy concerns in recommendation systems?

Recommendation systems raise privacy concerns because they collect detailed behavioural data (views, purchases, locations, searches) from which sensitive traits such as health, sexuality, religion, politics or finances can be inferred, and because that data can leak through breaches, sharing with third parties, de-anonymization or the recommendations themselves. Addressing them means collecting only what is needed with clear consent, protecting and limiting access to data, respecting laws such as the GDPR and CCPA, and using privacy-preserving techniques such as differential privacy, federated learning and on-device processing.

Read full answer

Applies togeneral concepts (GDPR, CCPA/CPRA; privacy-preserving machine learning)

Main Concerns

Concern Example
Sensitive inference Purchase history suggests a pregnancy or illness
Re-identification “Anonymized” rating data linked to public profiles (the Netflix Prize dataset de-anonymization study)
Leakage via recommendations Shared-device or “people you may know” suggestions reveal contacts or interests
Data breaches Stored histories exposed
Third-party sharing and tracking Behaviour shared with advertisers or data brokers
Lack of consent and transparency Users don’t know what’s collected or why they see an item
Membership and model inversion attacks Inferring whether someone was in the training data
Retention Data kept longer than needed

Legal and Policy Requirements

  • Lawful basis and consent for processing personal data; explicit consent for special-category data under the GDPR.

  • User rights: access, deletion (“right to be forgotten”), objection to profiling, data portability.

  • Purpose limitation and data minimization.

  • Transparency: explain why items are recommended; the EU Digital Services Act requires large platforms to explain main recommender parameters and offer an option not based on profiling.

Privacy-Preserving Techniques

  • Data minimization and retention limits: collect less, aggregate, delete old raw logs.

  • Pseudonymization and access control: separate identities from behaviour; least-privilege access; encryption at rest and in transit.

  • Differential privacy: add calibrated noise to training (DP-SGD) or aggregates so no individual’s data changes outputs much.

  • Federated learning: train on devices and share only model updates.

  • On-device recommendation for the most sensitive signals.

  • Sensitive-category controls: exclude or require opt-in for health, religion and similar topics.

  • User controls: pause history, remove items, “not interested”, turn off personalization.

Common Pitfalls

  • Assuming removing names anonymizes behavioural data.

  • Deleting a user’s raw data but leaving it in trained models and feature stores without a retraining plan.

  • Logging more than needed “just in case”.

Interview Tips

  • Mention the Netflix Prize de-anonymization as the classic example.

  • Pair each risk with a concrete control (consent, minimization, differential privacy, federated learning).

20

How do you evaluate a recommendation system's performance?

A recommendation system is evaluated offline with accuracy and ranking metrics on held-out historical data (precision@k, recall@k, MAP, NDCG, hit rate, and RMSE for rating prediction), with beyond-accuracy metrics such as coverage, diversity and novelty, and online with A/B tests that measure business outcomes such as click-through, conversion, watch time and retention. Offline evaluation is fast but biased by what the old system showed, so promising models are confirmed online; user studies and qualitative review complete the picture.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Offline Metrics

Metric What it measures
RMSE / MAE Rating prediction error (explicit feedback)
Precision@k Share of the top k that are relevant
Recall@k Share of relevant items found in the top k
Hit rate@k Share of users with at least one relevant item in the top k
MAP Mean of average precision; rewards relevant items ranked early
NDCG@k Graded relevance with a logarithmic position discount, normalized by the ideal ranking
MRR Reciprocal rank of the first relevant item
Coverage Share of the catalogue ever recommended
Diversity / novelty Dissimilarity within lists; how un-popular recommended items are

Offline Protocol

  • Time-based split: train on interactions before a date, test on those after; or leave-last-out per user.

  • Rank against all items or against sampled negatives (sampled metrics can change model rankings, so prefer full ranking when feasible).

  • Compare to baselines: popularity and item-kNN.

  • Report by segment: new vs heavy users, new items.

Online Evaluation

  • A/B tests: randomize users, measure primary business metrics and guardrails (latency, complaints, diversity).

  • Interleaving: mix two rankers’ results in one list; more sensitive for ranking comparisons.

  • Long-term metrics: retention and satisfaction, not only clicks.

  • Counterfactual (off-policy) evaluation with logged propensities, using inverse propensity scoring.

Code Example

import numpy as np

def precision_recall_ndcg(recommended, relevant, k):
    top = recommended[:k]
    hits = [1.0 if item in relevant else 0.0 for item in top]
    precision = sum(hits) / k
    recall = sum(hits) / len(relevant)
    dcg = sum(h / np.log2(rank + 2) for rank, h in enumerate(hits))
    ideal = sum(1 / np.log2(rank + 2) for rank in range(min(len(relevant), k)))
    return precision, recall, float(dcg / ideal)

recommended = [10, 3, 7, 2, 9]
relevant = {3, 9, 4}
p, r, n = precision_recall_ndcg(recommended, relevant, k=5)
print(round(p, 2), round(r, 2), round(n, 3))   # 0.4 0.67 ...; hits at ranks 2 and 5

Common Pitfalls

  • Random splits that leak the future.

  • Only reporting RMSE for a top-N ranking product.

  • Trusting offline gains without an online test; feedback loops make logged data biased.

Interview Tips

  • Explain NDCG’s position discount clearly.

  • Always name the business metric and a guardrail for the A/B test.

21

Why is A/B testing important when deploying recommendation systems?

A/B testing is important for recommendation systems because offline metrics computed on logged data often fail to predict real impact: the logs only contain what the old system showed, users react to the new recommendations themselves, and the goals that matter (engagement, conversion, retention, revenue) can only be measured live. In an A/B test, users are randomly split between the current recommender (control) and the new one (treatment), and the difference in pre-registered metrics is tested for statistical significance before a full rollout.

Read full answer

Applies togeneral concepts (online experimentation)

Why Offline Isn’t Enough

  • Selection bias: logged interactions reflect the old model’s exposure.

  • Feedback loops: new recommendations change what users click.

  • Metric mismatch: NDCG on history ≠ long-term satisfaction or revenue.

  • System effects: latency, diversity and freshness change behaviour.

  • Segment effects: new users, heavy users and markets may respond differently.

Designing the Test

Element Recommendation
Unit of randomization User (consistent experience); not request or session for personalization
Primary metric One business metric, e.g. conversion or watch time per user
Guardrails Latency, error rate, unsubscribes, complaints, diversity, revenue
Sample size Power analysis from baseline rate, minimum detectable effect, α = 0.05, power 0.8
Duration Full weekly cycles; long enough to pass novelty effects
Analysis Pre-registered tests, confidence intervals, CUPED for variance reduction

Pitfalls Specific to Recommenders

  • Novelty and primacy effects: users click new items at first, then settle; run longer or check the trend.

  • Interference: shared inventory, marketplaces or social networks let treatment affect control (use cluster or switchback designs).

  • Peeking: stopping as soon as p < 0.05 inflates false positives; use fixed horizons or sequential tests.

  • Short-term vs long-term: click-through can rise while retention falls; keep long-running holdouts.

  • Multiple metrics: correct for many comparisons or pick a primary metric in advance.

Complements to A/B Tests

  • Interleaving: blend two rankers in one list and see which gets more clicks; detects ranking differences with far fewer users.

  • Off-policy evaluation: estimate a new policy’s value from logs with inverse propensity scoring before testing.

  • Bandits: allocate traffic adaptively when exploration cost matters more than clean inference.

Example Decision Flow

  1. Offline: the model beats the baseline on recall@k and NDCG on a time-based split.

  2. Shadow mode: check latency and prediction sanity on live traffic.

  3. A/B test at 5-10% of users with guardrails.

  4. Ramp up if the primary metric improves significantly and guardrails hold.

  5. Keep a small holdout for long-term effects.

Common Pitfalls

  • Randomizing by page view, so users see a mix of systems.

  • Declaring success on clicks alone.

  • Ignoring sample ratio mismatch (unequal group sizes signal a bug).

Interview Tips

  • Name the primary metric, guardrails and the randomization unit explicitly.

  • Mention novelty effects and interleaving; interviewers value recommender-specific awareness.

22

How do recommendation systems handle changing user preferences over time?

Recommendation systems handle changing preferences (temporal dynamics, or preference drift) by weighting recent behaviour more than old behaviour, modelling short-term session intent separately from long-term taste, adding time and context features, using sequence models that read the order of recent interactions, and retraining or updating models frequently. Seasonal and trending effects are captured with time-aware features, while exploration helps detect new interests that the history doesn’t yet show.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Kinds of Change

Change Example Approach
Gradual drift Tastes evolve over months Time decay, time-aware biases
Life events New baby, moving house Fast profile updates, session signals
Seasonality Holidays, sports seasons Time-of-year and day-of-week features
Session intent Shopping for a gift today Session-based models
Trends A viral song Fresh popularity signals, frequent index updates
Item drift An item’s appeal fades Time-dependent item biases

Techniques

  • Time decay: weight each interaction by exp(−λ·age) or a half-life.

  • Sliding windows: train only on recent data, or combine short- and long-window profiles.

  • Time-aware matrix factorization: timeSVD++ adds user and item biases that change over time.

  • Sequence models: GRU4Rec, SASRec and BERT4Rec predict the next item from recent history.

  • Short-term + long-term embeddings: concatenate a session embedding with a long-term user embedding in the ranker.

  • Online learning and frequent retraining: hourly or daily updates; streaming feature updates.

  • Exploration: bandits test new interests rather than repeating old ones.

  • Explicit controls: “not interested” and history removal let users correct the profile.

Code Example

import numpy as np

# Interactions: (item, days ago); the user liked thrillers long ago and comedies recently
history = [(0, 300), (1, 280), (2, 5), (3, 2)]
genres = np.array([[1, 0], [1, 0], [0, 1], [0, 1]], dtype=float)   # [thriller, comedy]
half_life = 30.0                                                    # days

weights = np.array([0.5 ** (age / half_life) for _, age in history])
profile = sum(w * genres[item] for w, (item, _) in zip(weights, history)) / weights.sum()
print("decayed profile [thriller, comedy]:", np.round(profile, 3).tolist())   # comedy dominates

Evaluating Under Drift

  • Use time-based splits, and evaluate on several consecutive periods.

  • Monitor online metrics and feature distributions for drift; trigger retraining when they shift.

Common Pitfalls

  • Too fast a decay that forgets stable long-term tastes.

  • Random train/test splits that hide drift.

  • Profiles that never forget a one-off purchase (one gift for someone else).

Interview Tips

  • Distinguish long-term preferences from short-term session intent.

  • Mention timeSVD++ and sequence models by name.

23

What metrics are used to evaluate collaborative filtering systems?

Collaborative filtering systems are evaluated with rating prediction metrics (RMSE and MAE) when they predict explicit ratings, and with top-N ranking metrics (precision@k, recall@k, hit rate, MAP, MRR and NDCG) when they produce ranked lists, which is the usual product goal; beyond-accuracy metrics such as catalogue coverage, diversity, novelty and popularity bias complete the evaluation. Online, the system is judged on business metrics in A/B tests, such as click-through, conversion and retention.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Rating Prediction Metrics

Metric Formula Note
MAE mean of abs(r − r̂) Easy to interpret, in rating units
RMSE sqrt(mean((r − r̂)²)) Penalizes large errors; Netflix Prize metric

Ranking Metrics

Metric What it measures
Precision@k Relevant items in top k / k
Recall@k Relevant items in top k / all relevant items
Hit rate@k Share of users with at least one hit in top k
MRR Mean of 1/rank of the first relevant item
MAP@k Mean of average precision, rewards early hits
NDCG@k DCG = Σ rel_i / log2(i + 1), divided by the ideal DCG
AUC Probability a relevant item is ranked above a non-relevant one

Beyond-Accuracy Metrics

  • Catalogue coverage: share of items recommended to anyone.

  • User coverage: share of users who get a recommendation.

  • Intra-list diversity: average dissimilarity within each list.

  • Novelty: mean −log2(popularity) of recommended items.

  • Popularity bias: average popularity of recommendations compared with the catalogue.

Code Example

import numpy as np

true = np.array([4, 3, 5, 2, 1], dtype=float)
pred = np.array([3.5, 3, 4, 3, 1], dtype=float)
mae = np.mean(np.abs(true - pred))
rmse = np.sqrt(np.mean((true - pred) ** 2))
print("MAE:", round(float(mae), 3), "RMSE:", round(float(rmse), 3))   # MAE 0.5, RMSE 0.671

def average_precision(recommended, relevant, k):
    hits, score = 0, 0.0
    for rank, item in enumerate(recommended[:k], start=1):
        if item in relevant:
            hits += 1
            score += hits / rank                  # precision at each hit
    return score / min(len(relevant), k)

print("AP@5:", round(average_precision([7, 1, 4, 2, 9], {1, 2}, 5), 3))   # (1/2 + 2/4) / 2 = 0.5

Protocol

  • Split by time or leave-last-out; never randomly shuffle interactions across time.

  • Use full ranking over all items where possible; sampled metrics (rank among 100 negatives) can mislead.

  • Compare with popularity and item-kNN baselines.

  • Report separately for cold users and long-tail items.

Common Pitfalls

  • Optimizing RMSE when the product shows a top-10 list; low RMSE doesn’t mean a good ranking.

  • Reporting only averages that hide poor results for light users.

  • Ignoring coverage: a model can score well by recommending only popular items.

Interview Tips

  • Explain why ranking metrics replaced RMSE after the Netflix Prize era.

  • Be able to compute precision@k and NDCG by hand on a small list.

24

How do content-based recommenders use natural language processing?

Content-based recommenders use natural language processing to turn item text (titles, descriptions, reviews, articles, transcripts, tags) into numeric representations, such as TF-IDF vectors, topic distributions or dense sentence embeddings, and then recommend items whose text representation is close to the user’s profile or to items the user liked. NLP also extracts entities, keywords, sentiment and categories that become features, and modern transformer embeddings capture meaning rather than only word overlap, which helps with synonyms and cold-start items.

Read full answer

Applies toPython 3.12, scikit-learn 1.5+; sentence-transformers 3.x for embeddings

NLP Techniques for Item Profiles

Technique Representation Notes
Bag of words / TF-IDF Sparse word weights Fast, explainable, misses synonyms
Topic models (LDA, NMF) Topic proportions Interpretable themes
Word embeddings (word2vec, GloVe) Averaged word vectors Some semantic similarity
Sentence embeddings (transformers) Dense vectors (e.g. 384-768 dimensions) Captures meaning; best default today
Named entity recognition People, places, brands Features and filters
Sentiment analysis Review polarity and aspects Quality and aspect signals
Keyword extraction / classification Tags, categories Structured features

How It Works

  1. Preprocess: clean text, lowercase, remove boilerplate; language detection for multilingual catalogues.

  2. Represent each item with TF-IDF or embeddings.

  3. Build the user profile: weighted average of liked items’ vectors (or embed the user’s search queries).

  4. Score: cosine similarity between profile and candidate items.

  5. Serve: ANN search over item embeddings for large catalogues.

Code Example

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

articles = [
    "Neural networks improve image recognition accuracy",
    "Stock markets rally as interest rates fall",
    "Deep learning models for speech recognition",
    "Central bank raises interest rates again",
]
vectors = TfidfVectorizer(stop_words="english").fit_transform(articles)

liked = [0]                                            # the user read article 0
profile = np.asarray(vectors[liked].mean(axis=0))
scores = cosine_similarity(profile, vectors).ravel()
scores[liked] = -1
print("recommend article:", int(np.argmax(scores)))   # 2 (shares "recognition")

# With semantic embeddings (sentence-transformers):
# from sentence_transformers import SentenceTransformer
# model = SentenceTransformer("all-MiniLM-L6-v2")
# emb = model.encode(articles, normalize_embeddings=True)

Benefits

  • Recommends new items immediately (no interactions needed).

  • Explainable (“because you read about speech recognition”).

  • Works across languages with multilingual embeddings.

Common Pitfalls

  • TF-IDF misses synonyms (“car” vs “automobile”); use embeddings.

  • Over-specialization: only very similar articles; add diversity and collaborative signals.

  • Boilerplate text (site names, legal footers) dominating similarity.

Interview Tips

  • Contrast TF-IDF with transformer embeddings and when each is enough.

  • Mention combining text embeddings with collaborative signals in a hybrid model.

25

What role do neighborhood models play in collaborative filtering?

Neighbourhood models are the memory-based family of collaborative filtering that predict a user’s preference for an item from the most similar users (user-based) or the most similar items (item-based), combining the neighbours’ ratings with similarity weights. They are simple, explainable (“because you liked X”), easy to update when new ratings arrive, and still strong baselines; item-based neighbourhoods power classic “customers who bought this also bought” features, and learned neighbourhood weights were combined with matrix factorization in Netflix Prize models.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

How They Work

  1. Similarity: cosine, Pearson correlation or adjusted cosine on co-rated entries.

  2. Neighbour selection: the top k most similar (often k = 20-50), optionally a similarity threshold.

  3. Prediction: weighted average of neighbours’ (mean-centred) ratings.

  4. Recommendation: rank unseen items by predicted score.

Role in Collaborative Filtering

Role Detail
Baseline Item-kNN is a standard baseline; tuned well it rivals many complex models
Explanation Neighbours give natural reasons for a recommendation
Incremental updates New ratings change similarities locally; no full retraining
Candidate generation Item-item similarity tables give fast candidates
Local patterns Captures strong pairwise relations that global factor models smooth over
Hybrid models Koren’s integrated model combined neighbourhood and factor terms

Improvements

  • Mean-centring and baseline predictors (μ + b_u + b_i) before averaging.

  • Shrinkage: sim × n / (n + λ), where n is the number of co-ratings, so few co-ratings don’t produce extreme similarity.

  • Significance weighting and case amplification.

  • Learned interpolation weights instead of raw similarities (Bell and Koren).

  • Sparse linear methods (SLIM): learn an item-item weight matrix by regression.

Code Example

import numpy as np

R = np.array([[5, 3, 0, 4],
              [4, 0, 4, 5],
              [1, 1, 5, 2],
              [5, 4, 1, 0]], dtype=float)

def shrunk_cosine(a, b, lam=2.0):
    mask = (a > 0) & (b > 0)
    n = int(mask.sum())
    if n == 0:
        return 0.0
    sim = a[mask] @ b[mask] / (np.linalg.norm(a[mask]) * np.linalg.norm(b[mask]))
    return float(sim * n / (n + lam))           # shrink similarities based on few co-ratings

def predict(user, item, k=2):
    sims = [(shrunk_cosine(R[:, item], R[:, j]), j) for j in range(R.shape[1])
            if j != item and R[user, j] > 0]
    top = sorted(sims, reverse=True)[:k]
    return sum(s * R[user, j] for s, j in top) / sum(abs(s) for s, _ in top)

print("user 0, item 2:", round(predict(0, 2), 2))

Limitations

  • Poor with very sparse data (few co-ratings).

  • Memory and compute grow with users × items; precompute and prune.

  • Cold start for new users and items.

Common Pitfalls

  • Not mean-centring, so harsh and generous raters distort predictions.

  • Using every neighbour rather than the top k.

  • Trusting similarities from one or two co-ratings.

Interview Tips

  • Mention shrinkage and baseline predictors; they show practical experience.

  • Contrast neighbourhood models (local) with matrix factorization (global).

26

How are Bayesian networks used in recommendation systems?

Bayesian networks are probabilistic graphical models, directed acyclic graphs whose nodes are random variables (user traits, item attributes, context, liking) and whose edges encode conditional dependencies; in recommendation they model how these variables influence each other so the system can compute the probability that a user will like an item given whatever evidence is known. They were used in early model-based collaborative filtering (Breese, Heckerman and Kadie, 1998), and they suit settings that need reasoning under uncertainty, explicit domain knowledge and explanations, such as knowledge-based and context-aware recommenders, though matrix factorization and deep models dominate large-scale systems today.

Read full answer

Applies togeneral concepts; examples in Python 3.12 (plain probability tables)

How It Works

  • Structure: a DAG, e.g. Context → Mood → Likes Genre → Likes Item; learned from data or designed by experts.

  • Parameters: conditional probability tables (CPTs), P(node | parents), estimated from interaction data with smoothing.

  • Inference: given evidence (observed ratings, context), compute P(Like = yes | evidence) with exact inference (variable elimination) or approximate methods.

  • Recommendation: rank items by the posterior probability of liking them.

Uses in Recommenders

Use Example
Model-based CF Each item is a node; other items’ ratings predict it
Context-aware Time, location or companion as parent nodes
Knowledge-based Expert rules encoded as dependencies (financial products, healthcare)
Handling missing data Inference works with partial evidence
Explanation Paths through the graph explain why
Hierarchical Bayesian models Share strength across users (related idea; e.g. Bayesian matrix factorization)

Code Example

# Tiny network: Weekend -> Mood (relaxed) -> Likes comedy
p_relaxed = {True: 0.8, False: 0.3}            # P(relaxed | weekend)
p_like = {True: 0.7, False: 0.2}               # P(likes comedy | relaxed)

def p_like_comedy(weekend):
    """Sum over the hidden Mood node (variable elimination by hand)."""
    pr = p_relaxed[weekend]
    return pr * p_like[True] + (1 - pr) * p_like[False]

print("weekend:", round(p_like_comedy(True), 2))   # 0.8*0.7 + 0.2*0.2 = 0.60
print("weekday:", round(p_like_comedy(False), 2))  # 0.3*0.7 + 0.7*0.2 = 0.35

For real networks, libraries such as pgmpy learn structure and CPTs and run inference.

Advantages and Limitations

  • Advantages: principled uncertainty, uses domain knowledge, handles missing data, interpretable.

  • Limitations: structure learning is hard and expensive, CPTs grow exponentially with parents, exact inference is intractable in large graphs, and they scale poorly to millions of items.

Common Pitfalls

  • Confusing Bayesian networks with naive Bayes (a special case with one parent class node).

  • Learning CPTs from sparse data without smoothing.

  • Assuming edges mean causation without a causal design.

Interview Tips

  • Mention the Breese et al. 1998 comparison of model-based (Bayesian network) and memory-based CF.

  • Position Bayesian networks for context and knowledge-heavy domains rather than web-scale retrieval.

27

What is a Restricted Boltzmann Machine and how is it used for recommendation?

A Restricted Boltzmann Machine (RBM) is a two-layer generative neural network with a visible layer and a hidden layer, connected by undirected weights with no connections within a layer, that learns a probability distribution over its inputs; for recommendation, the visible units are a user’s ratings and the hidden units learn latent features, so the model can reconstruct (predict) the ratings the user hasn’t given. Salakhutdinov, Mnih and Hinton (2007) applied RBMs to the Netflix Prize, where they performed about as well as matrix factorization and the blend of both improved results; today RBMs are mostly of historical interest, replaced by autoencoders and other deep models.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Structure

  • Visible units v: one per item (binary like/not, or softmax units over 1-5 stars in the Netflix model).

  • Hidden units h: binary latent features (for example “likes action”).

  • Energy: E(v, h) = −aᵀv − bᵀh − vᵀWh; probability ∝ exp(−E).

  • Because there are no within-layer links, p(h | v) and p(v | h) factorize, so sampling a whole layer is easy.

Training and Prediction

  • Contrastive divergence (CD-k): increase the probability of the data and decrease that of reconstructions: ΔW ∝ ⟨v hᵀ⟩_data − ⟨v hᵀ⟩_reconstruction.

  • Per-user RBMs with shared weights: each user’s RBM has visible units only for the items they rated; weights are shared across users, which handles missing ratings.

  • Prediction: compute hidden activations from known ratings, then the visible probabilities for unrated items; rank by expected rating.

Code Example

import numpy as np

rng = np.random.default_rng(42)
V = np.array([[1, 1, 1, 0, 0, 0],
              [1, 0, 1, 0, 0, 0],
              [0, 0, 1, 1, 1, 0],
              [0, 0, 0, 1, 1, 1]], dtype=float)   # users x items, 1 = liked
n_hidden, lr = 2, 0.1
W = rng.normal(0, 0.1, (V.shape[1], n_hidden))
a, b = np.zeros(V.shape[1]), np.zeros(n_hidden)
sigmoid = lambda x: 1 / (1 + np.exp(-x))

for epoch in range(2000):                           # CD-1
    ph = sigmoid(V @ W + b)
    h = (rng.random(ph.shape) < ph).astype(float)
    pv = sigmoid(h @ W.T + a)                       # reconstruction
    ph2 = sigmoid(pv @ W + b)
    W += lr * (V.T @ ph - pv.T @ ph2) / len(V)
    a += lr * (V - pv).mean(axis=0)
    b += lr * (ph - ph2).mean(axis=0)

user = V[1]
scores = sigmoid(sigmoid(user @ W + b) @ W.T + a)   # visible probabilities
scores[user > 0] = -1
print("recommend item:", int(np.argmax(scores)))   # learned from training; likely item 1

Advantages and Limitations

  • Advantages: models non-linear structure, handles missing ratings naturally, complements MF in ensembles.

  • Limitations: harder to train and tune (CD, sampling), slower than MF, largely superseded by autoencoders, VAEs and neural CF.

Common Pitfalls

  • Treating unrated items as zeros in the visible layer instead of leaving them out.

  • Expecting stable results without tuning learning rate and hidden size.

Interview Tips

  • Mention the Netflix Prize RBM paper and its role in the winning ensemble.

  • Compare RBMs with autoencoders, which do a similar reconstruction with simpler training.

28

How does reinforcement learning apply to adaptive recommendation systems?

Reinforcement learning (RL) treats recommendation as a sequential decision problem: the recommender is an agent that observes the user’s state (history, context), takes an action (which item or slate to show), receives a reward (click, watch time, purchase, return visit) and learns a policy that maximizes long-term cumulative reward rather than the next click. This lets it optimize long-term engagement and satisfaction, balance exploration of new items with exploitation, and adapt to each user’s responses; contextual bandits are the simplest and most widely deployed form, while full RL (for example off-policy REINFORCE at YouTube or slate RL methods) handles longer-term effects.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

RL Formulation

Element In a recommender
State User history, profile embedding, session context, time
Action An item or a slate (ranked list) to show
Reward Click, dwell time, purchase, rating; long-term return or retention
Transition How the user’s state changes after seeing the recommendation
Policy The recommender’s strategy for picking items
Discount γ Weights future engagement

Approaches

  • Multi-armed and contextual bandits: one-step decisions with exploration (epsilon-greedy, UCB, Thompson sampling, LinUCB); used for news, ads and new-item exploration.

  • Value-based deep RL: DQN-style models estimate Q(state, item); slate methods such as SlateQ decompose slate value into item values.

  • Policy gradient: off-policy REINFORCE with importance weighting to learn from logged data (Chen et al., 2019, YouTube).

  • Offline RL / counterfactual learning: learn from logs safely before deployment, with inverse propensity scoring.

Code Example

import numpy as np

rng = np.random.default_rng(42)
true_ctr = np.array([0.05, 0.12, 0.08])         # unknown click rates of three items
alpha, beta = np.ones(3), np.ones(3)             # Beta(1, 1) priors

for _ in range(5000):                            # Thompson sampling bandit
    item = int(np.argmax(rng.beta(alpha, beta)))
    click = rng.random() < true_ctr[item]
    alpha[item] += click
    beta[item] += 1 - click

shows = alpha + beta - 2
print("times shown:", shows.astype(int).tolist())   # most impressions go to item 1

Challenges

  • Huge action spaces: millions of items; use candidate generation first.

  • Offline training: online exploration is costly, so learning from biased logs needs off-policy correction.

  • Reward design: clicks alone encourage clickbait; use satisfaction and long-term signals.

  • Simulation: user simulators (for example RecSim) help but may not match real users.

  • Safety: guard against harmful feedback loops and manipulative optimization.

Common Pitfalls

  • Applying full RL where a contextual bandit suffices.

  • Ignoring that logged data was produced by another policy (needs propensities).

  • Optimizing short-term engagement metrics that hurt user well-being.

Interview Tips

  • Start with bandits, then explain when long-term RL is worth it.

  • Mention off-policy correction and reward design as the hard parts.

29

How are Gradient Boosting Machines used in recommendation engines?

Gradient Boosting Machines (GBMs) such as XGBoost, LightGBM and CatBoost are used mainly in the ranking stage of recommendation engines: after candidate generation retrieves a few hundred items, a GBM scores each user-item pair from rich tabular features (user history, item popularity, collaborative-filtering scores, context, recency) to predict a click, purchase or relevance label, and the candidates are sorted by that score. GBMs handle heterogeneous features and missing values well, train fast, and support learning-to-rank objectives such as LambdaMART, which directly optimizes ranking metrics like NDCG.

Read full answer

Applies toPython 3.12, LightGBM 4.x (`LGBMRanker`), XGBoost 2.x, scikit-learn 1.5+

Where GBMs Fit

Stage Role of GBM
Candidate generation Rarely (too slow to score all items)
Ranking Main use: pointwise (click probability) or listwise/pairwise (LambdaMART)
Re-ranking Sometimes, with business features
Feature stacking Uses outputs of MF, embeddings, content similarity as input features

Typical Features

  • User: activity counts, average rating, preferred categories, recency of last visit.

  • Item: popularity, average rating, age, price, category.

  • User-item: MF dot product, embedding cosine, past interactions with the item’s category or brand.

  • Context: time of day, device, position, query.

Objectives

  • Pointwise: binary classification (clicked or not) with log loss.

  • Pairwise / listwise: objective="lambdarank" in LightGBM or rank:ndcg in XGBoost; data grouped by user or query.

Code Example

import numpy as np
from lightgbm import LGBMRanker

rng = np.random.default_rng(42)
n_users, per_user = 200, 10
X = rng.normal(size=(n_users * per_user, 4))        # features: mf_score, popularity, recency, price
relevance = (X[:, 0] + 0.5 * X[:, 1] + rng.normal(0, 0.5, len(X)) > 0.8).astype(int)
group = [per_user] * n_users                        # candidates per user, rows grouped by user

ranker = LGBMRanker(objective="lambdarank", n_estimators=200, learning_rate=0.05,
                    random_state=42, verbose=-1)
ranker.fit(X, relevance, group=group)

new_candidates = rng.normal(size=(5, 4))
order = np.argsort(-ranker.predict(new_candidates))
print("ranked candidates:", order.tolist())

Advantages and Limitations

  • Advantages: strong on tabular features, interpretable (feature importance, SHAP), quick to train, robust to scaling.

  • Limitations: can’t learn embeddings from raw IDs or sequences itself, needs feature engineering, slow to score millions of items.

Common Pitfalls

  • Leakage: features computed with data from after the label’s time.

  • Forgetting the group structure for ranking objectives.

  • Training only on shown items without accounting for position bias.

Interview Tips

  • Describe the two-stage pipeline with a GBM ranker over MF and embedding features.

  • Mention LambdaMART and NDCG optimization.

30

How is K-means clustering used for user segmentation in recommendations?

K-means clustering groups users into k segments with similar behaviour or features (for example genre preferences, spending, activity level) by repeatedly assigning each user to the nearest cluster centre and moving each centre to the mean of its users; recommendations are then made per segment, such as the most popular items within a user’s cluster, or the segment is used as a feature and a candidate source for a personalized model. Segmentation reduces sparsity and computation, helps cold-start users once they can be assigned to a segment, and gives interpretable groups for marketing and analysis.

Read full answer

Applies toPython 3.12, scikit-learn 1.5+, NumPy 2.x

How It Works

  1. Build user features: genre or category shares, activity counts, recency, spending, or user embeddings from matrix factorization.

  2. Scale features (StandardScaler) so no single feature dominates.

  3. Choose k with the elbow method, silhouette score or business needs.

  4. Fit K-means and assign each user to a cluster.

  5. Recommend: rank items by popularity or average rating within the cluster, excluding items the user has seen.

  6. Refresh clusters regularly as behaviour changes.

Code Example

import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# User features: share of [action, comedy, documentary] viewing
users = np.array([[0.8, 0.1, 0.1], [0.7, 0.2, 0.1], [0.1, 0.8, 0.1],
                  [0.2, 0.7, 0.1], [0.1, 0.1, 0.8], [0.0, 0.2, 0.8]])
X = StandardScaler().fit_transform(users)
km = KMeans(n_clusters=3, n_init=10, random_state=42).fit(X)
labels = km.labels_

# Interactions: users x 5 items (1 = watched)
watched = np.array([[1, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 1, 1, 0],
                    [0, 0, 1, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 1]])

def recommend(user, n=1):
    peers = labels == labels[user]
    popularity = watched[peers].sum(axis=0).astype(float)
    popularity[watched[user] > 0] = -1            # skip items already watched
    return [int(i) for i in np.argsort(-popularity, kind="stable")[:n]]

print("user 1 gets:", recommend(1))   # [1]: watched by the other action fan
print("user 3 gets:", recommend(3))   # [3]: watched by the other comedy fan

Advantages and Limitations

  • Advantages: simple, scalable, interpretable segments, reduces sparsity, useful for cold start once a few signals exist.

  • Limitations: coarse (everyone in a segment gets similar items), assumes spherical clusters of similar size, hard assignments ignore mixed tastes, k must be chosen.

Alternatives

  • Gaussian mixture models for soft membership.

  • Hierarchical clustering for nested segments.

  • Clustering embeddings from a collaborative model rather than raw features.

Common Pitfalls

  • Not scaling features.

  • Using segment popularity as the only recommender; combine with personalization.

  • Stale clusters that no longer reflect behaviour.

Interview Tips

  • Explain segmentation as a candidate source or feature, not a replacement for personalized ranking.

  • Mention silhouette score for choosing k.

31

How do you implement a content-based recommender in Python?

A simple content-based recommender represents each item by its features, typically TF-IDF vectors of its text plus encoded categories, builds a profile for the user from the items they liked, and recommends the unseen items whose vectors are most similar (by cosine similarity) to that profile. It needs no other users’ data, works for new items immediately, and can explain each recommendation by the shared features.

Read full answer

Applies toPython 3.12, scikit-learn 1.5+, pandas 2.x, NumPy 2.x

Steps

  1. Item features: combine text fields (title, genres, description) into one document per item.

  2. Vectorize with TfidfVectorizer (stop words removed, optional n-grams).

  3. User profile: rating-weighted average of the vectors of items the user liked.

  4. Score every item by cosine similarity to the profile.

  5. Filter items already seen and return the top N.

Code Example

import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

movies = pd.DataFrame({
    "title": ["Star Voyage", "Galaxy Wars", "Love in Rome", "Space Rescue", "Paris Hearts"],
    "genres": ["sci-fi adventure", "sci-fi action", "romance comedy", "sci-fi drama", "romance drama"],
    "plot": ["crew explores distant planets", "rebels fight an empire in space",
             "two strangers fall in love on holiday", "astronauts stranded on a moon",
             "an artist finds love in paris"],
})
docs = movies["genres"] + " " + movies["plot"]
tfidf = TfidfVectorizer(stop_words="english")
item_vectors = tfidf.fit_transform(docs)                 # one row per movie

def recommend(ratings, n=2):
    """ratings: {movie index: rating 1-5} for the user."""
    liked = {i: r for i, r in ratings.items() if r >= 4}
    if not liked:
        return []                                         # cold start: fall back to popularity
    idx, weights = list(liked), np.array(list(liked.values()), dtype=float)
    profile = np.asarray(item_vectors[idx].T @ weights / weights.sum()).reshape(1, -1)
    scores = cosine_similarity(profile, item_vectors).ravel()
    scores[list(ratings)] = -1                            # skip already rated movies
    top = np.argsort(-scores)[:n]
    return movies.loc[top, "title"].tolist()

print(recommend({0: 5, 1: 4}))    # sci-fi fan: "Space Rescue" first

Design Choices

Choice Options
Text representation TF-IDF (fast) or sentence embeddings (semantic)
Profile Weighted average; separate positive and negative profiles
Similarity Cosine (standard for TF-IDF and embeddings)
Extra features One-hot categories, numeric features scaled, combined with weights
Scale Precompute vectors; ANN search for large catalogues

Extensions

  • Subtract a profile of disliked items (Rocchio-style).

  • Use sentence-transformer embeddings for synonyms.

  • Add diversity re-ranking so results aren’t all near-duplicates.

Common Pitfalls

  • Recommending items already rated.

  • Text with boilerplate dominating similarity.

  • No fallback for users with no liked items.

Interview Tips

  • Explain how the user profile is built; it’s the step most often skipped.

  • Mention over-specialization and how a hybrid or diversity re-ranking fixes it.

32

How do you build a collaborative filtering recommender with the Surprise library?

Surprise (scikit-surprise) is a Python library for explicit-rating recommenders: you load ratings into a Dataset with a Reader that sets the rating scale, pick an algorithm such as SVD (matrix factorization) or KNNWithMeans (neighbourhood CF), evaluate it with cross_validate or a train/test split using RMSE and MAE, then train on the full data and call predict(user, item) for every unrated item to build top-N lists. It is ideal for learning and prototyping; it doesn’t support implicit feedback or very large data, where libraries such as implicit, LightFM or Spark ALS are used.

Read full answer

Applies toPython 3.12, scikit-surprise 1.1.4, pandas 2.x

Main Algorithms

Class Type
NormalPredictor, BaselineOnly Baselines (random from distribution, μ + b_u + b_i)
KNNBasic, KNNWithMeans, KNNWithZScore, KNNBaseline User- or item-based neighbourhoods
SVD, SVDpp, NMF Matrix factorization
SlopeOne, CoClustering Other CF methods

Code Example

from collections import defaultdict

import pandas as pd
from surprise import SVD, Dataset, KNNWithMeans, Reader, accuracy
from surprise.model_selection import cross_validate, train_test_split

ratings = pd.DataFrame({
    "user": ["u1", "u1", "u1", "u2", "u2", "u3", "u3", "u3", "u4", "u4"],
    "item": ["a", "b", "c", "a", "c", "b", "c", "d", "a", "d"],
    "rating": [5, 3, 4, 4, 5, 2, 4, 5, 3, 4],
})
data = Dataset.load_from_df(ratings[["user", "item", "rating"]], Reader(rating_scale=(1, 5)))

# Compare two algorithms with 3-fold cross-validation
for algo in [SVD(random_state=42), KNNWithMeans(sim_options={"name": "cosine", "user_based": False})]:
    results = cross_validate(algo, data, measures=["RMSE", "MAE"], cv=3, verbose=False)
    print(type(algo).__name__, "RMSE:", round(results["test_rmse"].mean(), 3))

# Train/test split and accuracy
trainset, testset = train_test_split(data, test_size=0.2, random_state=42)
model = SVD(n_factors=20, n_epochs=30, random_state=42).fit(trainset)
accuracy.rmse(model.test(testset))

# Top-N: predict every unrated (user, item) pair from the full data
full = data.build_full_trainset()
model = SVD(n_factors=20, n_epochs=30, random_state=42).fit(full)
anti = full.build_anti_testset()                 # pairs the users have not rated
top_n = defaultdict(list)
for pred in model.test(anti):
    top_n[pred.uid].append((pred.iid, round(pred.est, 2)))
for uid, items in top_n.items():
    print(uid, sorted(items, key=lambda x: -x[1])[:2])

Tuning

  • GridSearchCV(SVD, {"n_factors": [20, 50], "lr_all": [0.005, 0.01], "reg_all": [0.02, 0.1]}, measures=["rmse"], cv=3).

  • For KNN, set sim_options ("cosine", "msd", "pearson", "pearson_baseline") and k.

Common Pitfalls

  • Using Surprise for implicit data (clicks); it is built for explicit ratings.

  • Judging top-N quality only by RMSE; also compute precision@k and recall@k.

  • build_anti_testset() on large data creates every unrated pair and can exhaust memory.

Interview Tips

  • Know Dataset.load_from_df, Reader(rating_scale=...), cross_validate and build_anti_testset.

  • Mention when to move to implicit, LightFM or Spark.

33

How do you use Non-negative Matrix Factorization (NMF) for recommendations?

Non-negative Matrix Factorization (NMF) approximates a non-negative user-item matrix R as the product of two non-negative matrices, W (users × k) and H (k × items), so each user is a non-negative mix of k latent “parts” and each item a non-negative loading on them; predicted scores are the entries of W·H. Because nothing can cancel out, the factors are often interpretable (a factor might correspond to a genre), which makes NMF popular for implicit feedback and topic-like decompositions. scikit-learn’s NMF treats zeros as observed values, so it fits implicit or filled matrices; for explicit ratings with missing entries, use a masked version such as Surprise’s NMF.

Read full answer

Applies toPython 3.12, scikit-learn 1.5+, NumPy 2.x

How It Works

  • Objective: minimize ‖R − WH‖² (Frobenius) or the Kullback-Leibler divergence, with W, H ≥ 0, optionally with L1/L2 regularization.

  • Solvers: multiplicative updates (Lee and Seung) or coordinate descent (scikit-learn default solver="cd").

  • Multiplicative update for Frobenius loss: H ← H ⊙ (WᵀR) / (WᵀWH), W ← W ⊙ (RHᵀ) / (WHHᵀ).

Code Example

import numpy as np
from sklearn.decomposition import NMF

# Implicit interactions (play counts) for 5 users x 6 songs; 0 = no plays
R = np.array([[5, 3, 0, 0, 1, 0],
              [4, 4, 1, 0, 0, 0],
              [0, 0, 5, 4, 0, 1],
              [0, 1, 4, 5, 0, 0],
              [1, 0, 0, 1, 5, 4]], dtype=float)

model = NMF(n_components=3, init="nndsvda", max_iter=500, random_state=42)
W = model.fit_transform(R)          # user factors (5 x 3)
H = model.components_               # item factors (3 x 6)
scores = W @ H

print("reconstruction error:", round(float(model.reconstruction_err_), 3))
user = 1
scores[user, R[user] > 0] = -np.inf  # hide songs already played
print("recommend for user 1:", int(np.argmax(scores[user])))
print("item factors:\n", np.round(H, 2))   # each row is a latent group of songs

Masked NMF for Explicit Ratings (Multiplicative Updates)

import numpy as np

rng = np.random.default_rng(42)
R = np.array([[5, 4, 0], [4, 0, 2], [0, 2, 5]], dtype=float)
M = (R > 0).astype(float)                 # 1 where a rating is observed
k, eps = 2, 1e-9
W, H = rng.random((3, k)), rng.random((k, 3))
for _ in range(500):
    H *= (W.T @ (M * R)) / (W.T @ (M * (W @ H)) + eps)
    W *= ((M * R) @ H.T) / ((M * (W @ H)) @ H.T + eps)
print(np.round(W @ H, 2))                 # missing cells are now predictions

NMF vs SVD-Style MF

Aspect NMF SVD / MF
Constraint W, H ≥ 0 None
Interpretability Parts-based, often readable Factors mix signs
Data Non-negative (counts, ratings) Any
Accuracy Similar or slightly lower Often slightly better with biases

Common Pitfalls

  • Feeding missing explicit ratings as zeros to scikit-learn’s NMF; it learns to predict zeros.

  • Forgetting random_state; results depend on initialization.

  • Too many components, which overfits sparse data.

Interview Tips

  • Explain why non-negativity gives interpretable factors.

  • Mention the missing-value issue and the masked alternative.

34

How do you code an item recommender using cosine similarity?

An item-to-item recommender with cosine similarity represents each item as a vector (its column of user interactions, or a feature or embedding vector), computes the cosine of the angle between every pair of items, cos(a, b) = a·b / (‖a‖‖b‖), and for a given item returns the other items with the highest similarity. This powers “similar items” and “customers also viewed” sections; with normalized vectors, the similarity matrix is a single matrix product, and for large catalogues it is precomputed or replaced by approximate nearest-neighbour search.

Read full answer

Applies toPython 3.12, NumPy 2.x, scikit-learn 1.5+, SciPy 1.13+

Code Example

import numpy as np
from scipy.sparse import csr_matrix
from sklearn.metrics.pairwise import cosine_similarity

items = ["laptop", "mouse", "keyboard", "novel", "cookbook"]
# Users x items purchase counts
interactions = csr_matrix(np.array([
    [1, 1, 1, 0, 0],
    [1, 1, 0, 0, 0],
    [0, 1, 1, 0, 0],
    [0, 0, 0, 1, 1],
    [0, 0, 0, 1, 0],
    [1, 0, 1, 0, 0],
]))

item_sim = cosine_similarity(interactions.T, dense_output=True)   # items x items
np.fill_diagonal(item_sim, 0)                                     # an item isn't similar to itself

def similar_items(name, n=2):
    i = items.index(name)
    top = np.argsort(-item_sim[i], kind="stable")[:n]
    return [(items[j], round(float(item_sim[i, j]), 3)) for j in top if item_sim[i, j] > 0]

print(similar_items("laptop"))     # mouse (0.667), keyboard (0.667): a tie, kept in item order
print(similar_items("novel"))      # cookbook (0.707)

def recommend_for_user(user_items, n=2):
    """Sum similarities to everything the user bought."""
    scores = item_sim[[items.index(x) for x in user_items]].sum(axis=0)
    for x in user_items:
        scores[items.index(x)] = -np.inf
    return [items[j] for j in np.argsort(-scores, kind="stable")[:n]]

print(recommend_for_user(["mouse"]))   # ['laptop', 'keyboard']

Variants

Variant Use
Plain cosine on interaction columns Implicit “also bought”
Adjusted cosine (subtract each user’s mean rating) Explicit ratings
Cosine on TF-IDF or embeddings Content-based “similar items”
Top-k pruning Keep 20-100 neighbours per item to save memory

Scaling

  • Keep matrices sparse and compute similarity on the item side.

  • Precompute and store the top-k neighbours per item offline.

  • Use FAISS or HNSW with normalized embeddings for millions of items (inner product = cosine).

Common Pitfalls

  • Leaving the diagonal at 1, so the item recommends itself.

  • Very popular items appearing similar to everything; consider normalization or penalizing popularity.

  • Dense n × n matrices for large catalogues.

Interview Tips

  • Show that cosine on L2-normalized vectors is a dot product.

  • Mention adjusted cosine for ratings and the diagonal trick.

35

How do you build user-based collaborative filtering from scratch in Python?

User-based collaborative filtering from scratch means computing a similarity between every pair of users from their co-rated items (Pearson correlation or cosine on mean-centred ratings), selecting the k most similar users who rated a target item, and predicting the rating as the user’s own mean plus the similarity-weighted average of those neighbours’ deviations from their means. Ranking all unrated items by predicted rating gives the top-N recommendations.

Read full answer

Applies toPython 3.12, NumPy 2.x

Prediction Formula

r̂(u, i) = r̄_u + Σ_{v ∈ N_k(u, i)} sim(u, v)·(r(v, i) − r̄_v) / Σ |sim(u, v)|

  • r̄_u: mean rating of user u.

  • N_k(u, i): the k users most similar to u who rated i.

Code Example

import numpy as np

R = np.array([
    [5, 4, 0, 4, 1],
    [4, 0, 4, 5, 1],
    [1, 1, 5, 0, 5],
    [5, 5, 4, 4, 0],
    [0, 1, 4, 1, 5],
], dtype=float)                               # users x items, 0 = unrated
rated = R > 0
means = R.sum(axis=1) / rated.sum(axis=1)

def pearson(u, v):
    common = rated[u] & rated[v]
    if common.sum() < 2:
        return 0.0                            # too few co-ratings to trust
    a, b = R[u, common] - means[u], R[v, common] - means[v]
    denom = np.linalg.norm(a) * np.linalg.norm(b)
    return float(a @ b / denom) if denom else 0.0

n_users = R.shape[0]
S = np.array([[pearson(u, v) if u != v else 0.0 for v in range(n_users)] for u in range(n_users)])

def predict(u, i, k=2):
    candidates = [v for v in range(n_users) if rated[v, i] and S[u, v] > 0]
    neighbours = sorted(candidates, key=lambda v: -S[u, v])[:k]
    if not neighbours:
        return float(means[u])                # fallback: the user's mean
    num = sum(S[u, v] * (R[v, i] - means[v]) for v in neighbours)
    den = sum(abs(S[u, v]) for v in neighbours)
    return float(means[u] + num / den)

def recommend(u, n=2):
    preds = {i: predict(u, i) for i in range(R.shape[1]) if not rated[u, i]}
    return sorted(preds.items(), key=lambda x: -x[1])[:n]

print([(i, round(p, 2)) for i, p in recommend(0)])   # user 0 has only item 2 unrated
print([(i, round(p, 2)) for i, p in recommend(4)])

Design Choices

Choice Typical value
Similarity Pearson (handles rating scale bias) or cosine on centred ratings
k 20-50 in real data
Minimum co-ratings 2-5, or shrink similarity by n / (n + λ)
Negative similarities Usually ignored for prediction

Complexity

  • Similarities: O(U² · I) in the worst case; precompute and use sparse data, or switch to item-based CF for many users.

Common Pitfalls

  • Using unrated zeros as ratings in the similarity.

  • Not mean-centring.

  • Trusting similarities from one co-rated item.

Interview Tips

  • Write the formula with mean-centring; interviewers check it.

  • Discuss why item-based CF scales better than user-based CF.

36

How do you build a deep learning recommendation model with TensorFlow Keras?

A deep learning recommender in Keras embeds user IDs and item IDs into dense vectors with Embedding layers, combines them (a dot product for matrix factorization, or a concatenation fed to dense layers for neural collaborative filtering), and trains the network to predict ratings or interaction probabilities from the observed user-item pairs. After training, scoring all items for a user and taking the top N gives recommendations; for large catalogues, the learned item embeddings go into an approximate nearest-neighbour index.

Read full answer

Applies toPython 3.12, TensorFlow 2.16+ with Keras 3, NumPy 2.x

Architecture

Part Detail
Inputs user index, item index (0..n−1)
Embeddings Embedding(n_users, d) and Embedding(n_items, d)
Interaction GMF branch: element-wise product; MLP branch: concatenation + Dense layers
Output Dense(1) for rating regression, or Dense(1, activation="sigmoid") for implicit clicks
Loss MSE for ratings, binary cross-entropy with sampled negatives for implicit data

Code Example

import numpy as np
import keras
from keras import layers

keras.utils.set_random_seed(42)
rng = np.random.default_rng(42)
n_users, n_items, n = 50, 40, 2000
users = rng.integers(0, n_users, n)
items = rng.integers(0, n_items, n)
ratings = rng.integers(1, 6, n).astype("float32")         # synthetic 1-5 ratings

user_in = keras.Input(shape=(1,), dtype="int32", name="user")
item_in = keras.Input(shape=(1,), dtype="int32", name="item")
u = layers.Flatten()(layers.Embedding(n_users, 16)(user_in))
v = layers.Flatten()(layers.Embedding(n_items, 16)(item_in))

gmf = layers.Multiply()([u, v])                            # matrix-factorization-like branch
mlp = layers.Dense(32, activation="relu")(layers.Concatenate()([u, v]))
mlp = layers.Dense(16, activation="relu")(mlp)
out = layers.Dense(1)(layers.Concatenate()([gmf, mlp]))    # predicted rating

model = keras.Model([user_in, item_in], out)
model.compile(optimizer=keras.optimizers.Adam(1e-3), loss="mse", metrics=["mae"])
model.fit({"user": users.reshape(-1, 1), "item": items.reshape(-1, 1)}, ratings, epochs=5, batch_size=64,
          validation_split=0.2, verbose=0)

# Top-3 items for user 7, excluding items already rated
seen = set(items[users == 7].tolist())
candidates = np.array([i for i in range(n_items) if i not in seen])
scores = model.predict({"user": np.full((len(candidates), 1), 7), "item": candidates.reshape(-1, 1)},
                       verbose=0).ravel()
print("top items:", candidates[np.argsort(-scores)[:3]].tolist())

The ratings here are random, so the model learns little; with real data the same code learns user and item embeddings.

Implicit Feedback Version

  • Labels: 1 for interactions, 0 for sampled non-interacted items (e.g. 4 negatives per positive).

  • Output sigmoid, loss binary_crossentropy; evaluate with hit rate@k and NDCG@k.

  • For retrieval at scale, use a two-tower model with in-batch negatives (TensorFlow Recommenders).

Common Pitfalls

  • IDs not mapped to contiguous 0..n−1 indexes.

  • Random splits that leak future behaviour; split by time.

  • No regularization or early stopping, which overfits rare users.

Interview Tips

  • Describe GMF + MLP (neural collaborative filtering) and when a dot product suffices.

  • Mention negative sampling for implicit data.

37

How do you write a Python script for item-item similarity recommendations?

An item-item recommender script builds a user-item matrix from interaction data, computes item-to-item similarities (adjusted cosine for ratings, cosine for implicit data), keeps the top k neighbours of each item, and recommends to each user the unseen items with the highest weighted sum of similarities to the items they rated. This is the classic Amazon approach: item similarities are stable, can be computed offline, and give easy explanations (“because you bought X”).

Read full answer

Applies toPython 3.12, pandas 2.x, NumPy 2.x

Steps

  1. Load ratings (user, item, rating) and pivot to a user × item matrix.

  2. Mean-centre each user’s ratings (adjusted cosine).

  3. Compute item-item cosine similarity on the centred columns.

  4. Keep the top k neighbours per item.

  5. Predict r̂(u, i) = Σ sim(i, j)·r(u, j) / Σ |sim(i, j)| over the user’s rated neighbours j.

  6. Return the top N unseen items per user.

Code Example

import numpy as np
import pandas as pd

ratings = pd.DataFrame({
    "user": ["ann", "ann", "ann", "bob", "bob", "cat", "cat", "cat", "dan", "dan"],
    "item": ["A", "B", "C", "A", "B", "B", "C", "D", "C", "D"],
    "rating": [5, 4, 1, 4, 5, 2, 5, 4, 4, 5],
})
matrix = ratings.pivot_table(index="user", columns="item", values="rating")   # NaN = unrated
centred = matrix.sub(matrix.mean(axis=1), axis=0).fillna(0.0)                # adjusted cosine

X = centred.to_numpy()
norms = np.linalg.norm(X, axis=0)
norms[norms == 0] = 1.0
sim = (X.T @ X) / np.outer(norms, norms)
np.fill_diagonal(sim, 0.0)
sim = pd.DataFrame(sim, index=matrix.columns, columns=matrix.columns)

def recommend(user, n=2, k=2):
    rated = matrix.loc[user].dropna()
    scores = {}
    for item in matrix.columns.difference(rated.index):
        neighbours = sim.loc[item, rated.index].nlargest(k)
        neighbours = neighbours[neighbours > 0]          # only positively similar items
        if neighbours.empty:
            continue
        scores[item] = float((neighbours * rated[neighbours.index]).sum() / neighbours.abs().sum())
    return sorted(scores.items(), key=lambda x: -x[1])[:n]

for user in matrix.index:
    print(user, [(item, round(score, 2)) for item, score in recommend(user)])

Productionizing

  • Compute similarities in a nightly batch job and store the top k neighbours per item in a key-value store.

  • At request time, fetch neighbours of the user’s recent items and aggregate.

  • Add filters: in stock, not already purchased, allowed in the user’s region.

Common Pitfalls

  • Using fillna(0) before centring, which treats missing as a low rating.

  • Including negatively similar items in the weighted average.

  • Very popular items dominating every list.

Interview Tips

  • Explain adjusted cosine and why centring by user mean matters.

  • Describe the offline/online split of this algorithm.

38

How do you build a recommender that combines user ratings and item metadata?

A recommender that combines ratings and metadata is a hybrid: it learns collaborative signals from the user-item rating matrix and content signals from item metadata (genres, tags, text, price), then blends them, either by mixing the scores of a collaborative model and a content model, or with a single model such as a factorization machine or LightFM that represents each item as the sum of embeddings of its ID and its features. The collaborative part personalizes well for items with many ratings, and the metadata part covers new and rarely rated items.

Read full answer

Applies toPython 3.12, NumPy 2.x, scikit-learn 1.5+

Approaches

Approach How When
Weighted blend score = w·CF + (1 − w)·content Quick, two existing models
Confidence-based weight w grows with an item’s number of ratings Mix of popular and new items
Feature-rich MF Item vector = ID embedding + Σ feature embeddings (LightFM) Cold start, one model
Ranker CF and content scores as features in a GBM Production ranking

Code Example

import numpy as np
from sklearn.preprocessing import MultiLabelBinarizer

rng = np.random.default_rng(42)
R = np.array([[5, 4, 0, 0, 0],
              [4, 5, 0, 1, 0],
              [0, 1, 5, 4, 0],
              [1, 0, 4, 5, 0]], dtype=float)      # item 4 is new: no ratings
genres = [["action", "sci-fi"], ["action"], ["drama"], ["drama", "romance"], ["action", "sci-fi"]]
F = MultiLabelBinarizer().fit_transform(genres).astype(float)

# Collaborative part: matrix factorization on observed ratings
k, lr, reg = 2, 0.02, 0.05
P, Q = rng.normal(0, 0.1, (4, k)), rng.normal(0, 0.1, (5, k))
users, items = np.nonzero(R)
for _ in range(1000):
    for u, i in zip(users, items):
        err = R[u, i] - P[u] @ Q[i]
        P[u], Q[i] = P[u] + lr * (err * Q[i] - reg * P[u]), Q[i] + lr * (err * P[u] - reg * Q[i])
cf = P @ Q.T

# Content part: similarity of each item's genres to the user's liked items' genres
profile = (R >= 4).astype(float) @ F
content = profile @ F.T / (np.linalg.norm(profile, axis=1, keepdims=True) * np.linalg.norm(F, axis=1) + 1e-9)
content = 1 + 4 * content                         # map 0..1 similarity onto the 1..5 rating scale

n_ratings = (R > 0).sum(axis=0)
w = n_ratings / (n_ratings + 2.0)                 # trust CF more for well-rated items
hybrid = w * cf + (1 - w) * content

user = 0
scores = np.where(R[user] > 0, -np.inf, hybrid[user])
print("user 0 ranking of unrated items:", [int(i) for i in np.argsort(-scores)[:3]])
# item 4 (new action/sci-fi film) ranks first via its metadata

Common Pitfalls

  • Blending scores on different scales without normalizing.

  • One global weight for all items, ignoring how many ratings each has.

  • Metadata that is missing or inconsistent across items; clean and normalize tags.

Interview Tips

  • Name LightFM or factorization machines as single-model hybrids.

  • Explain how metadata solves item cold start.

39

How do you recommend items using Pearson correlation on a ratings matrix?

Pearson correlation measures how linearly two users’ ratings move together on the items both rated, after subtracting each user’s mean, so it corrects for users who rate everything high or low; a recommender uses it to find the users most correlated with the target user and predicts unrated items from their mean-centred ratings, weighted by correlation. Only positively correlated neighbours with enough co-rated items are normally used, and the top-scoring unrated items are recommended.

Read full answer

Applies toPython 3.12, NumPy 2.x

The Formula

  • Over co-rated items I_uv: sim(u, v) = Σ (r_ui − r̄_u)(r_vi − r̄_v) / sqrt(Σ (r_ui − r̄_u)² · Σ (r_vi − r̄_v)²).

  • r̄ can be the mean over co-rated items (textbook Pearson) or over all of a user’s ratings; be consistent.

  • Range −1 to 1: 1 = same taste pattern, 0 = unrelated, −1 = opposite.

  • Prediction: r̂(u, i) = r̄_u + Σ sim(u, v)(r_vi − r̄_v) / Σ |sim(u, v)| over the top-k neighbours who rated i.

Code Example

import numpy as np

R = np.array([
    [5, 3, 4, 4, 0],
    [3, 1, 2, 3, 3],
    [4, 3, 4, 3, 5],
    [3, 3, 1, 5, 4],
    [1, 5, 5, 2, 1],
], dtype=float)                      # 0 = unrated; user 0 hasn't rated item 4

def pearson(u, v):
    common = (R[u] > 0) & (R[v] > 0)
    if common.sum() < 2:
        return 0.0
    a = R[u, common] - R[u, common].mean()      # centre on co-rated items
    b = R[v, common] - R[v, common].mean()
    denom = np.sqrt((a ** 2).sum() * (b ** 2).sum())
    return float((a * b).sum() / denom) if denom else 0.0

def predict(u, i, k=2):
    mean_u = R[u][R[u] > 0].mean()
    sims = [(pearson(u, v), v) for v in range(len(R)) if v != u and R[v, i] > 0]
    top = [(s, v) for s, v in sorted(sims, reverse=True)[:k] if s > 0]
    if not top:
        return float(mean_u)
    num = sum(s * (R[v, i] - R[v][R[v] > 0].mean()) for s, v in top)
    return float(mean_u + num / sum(s for s, _ in top))

print("sim(0,1):", round(pearson(0, 1), 3))   # 0.853: same pattern, user 1 rates lower
print("sim(0,4):", round(pearson(0, 4), 3))   # -0.792: opposite taste
print("predicted rating of item 4 for user 0:", round(predict(0, 4), 2))

Pearson vs Cosine

Aspect Pearson Cosine
Centring Subtracts each user’s mean No (unless adjusted cosine)
Rating scale bias Corrected Not corrected
Few co-ratings Unstable Also unstable
Implicit binary data Less suitable Common choice

Common Pitfalls

  • Computing correlation over all items with zeros as ratings.

  • Trusting correlations based on two or three co-rated items; use significance weighting (multiply by min(n, 50)/50).

  • Including negatively correlated users in a weighted average without care.

Interview Tips

  • Explain why Pearson suits explicit ratings: it removes personal rating bias.

  • Mention significance weighting for small overlaps.

40

How would you build a recommendation system for an e-commerce platform?

An e-commerce recommender is built as a multi-stage system: collect behavioural events (views, add-to-carts, purchases, searches) and catalogue data, generate candidates from several sources (item-to-item co-purchase similarity, embedding retrieval, trending and category popularity), rank them with a model that predicts purchase or click probability from user, item and context features, then re-rank for diversity, stock, margin and business rules, and finally measure success with A/B tests on conversion and revenue. Different placements (home page, product page, cart, email) use different objectives, such as “similar items” versus “frequently bought together”.

Read full answer

Applies togeneral system design (ANN retrieval, gradient boosting or deep ranking, A/B testing)

Requirements and Placements

Placement Goal Typical method
Home page Personalized discovery User embedding retrieval + ranker
Product page “similar items” Alternatives Content + co-view similarity
“Frequently bought together” Complements, basket size Co-purchase association rules
Cart / checkout Add-ons Complements with low price
Search Relevance + personalization Learning-to-rank
Email / push Re-engagement Recently viewed, price drops

Architecture

  1. Data: event stream (Kafka), catalogue service, user profiles; signals weighted (view < cart < purchase).

  2. Candidate generation (hundreds of items):

  3. Item-item similarity from co-views and co-purchases.

  4. Two-tower model embeddings with ANN search (FAISS, ScaNN).

  5. Popular and trending items per category and region (cold start).

  6. Recently viewed items and their neighbours.

  7. Ranking: LightGBM LambdaMART or a deep CTR model (DCN, DeepFM) predicting purchase/click; features: user history, item price, rating, stock, CF scores, context.

  8. Re-ranking: remove out-of-stock and already purchased items (except consumables), diversify categories, apply margin or sponsored rules, cap repeated brands.

  9. Serving: low-latency API (tens of milliseconds), feature store for online features, caching.

  10. Feedback loop: log impressions, positions and outcomes for retraining.

Cold Start

  • New users: trending by region, category onboarding, session-based recommendations from the first clicks.

  • New products: content embeddings (text, images), category popularity, exploration slots.

Evaluation

  • Offline: recall@k, NDCG@k and hit rate on a time-based split; coverage and diversity.

  • Online A/B: conversion rate, revenue per session, average order value, click-through; guardrails: latency, return rates.

Common Pitfalls

  • Recommending items the user just bought (another TV) instead of complements.

  • Optimizing clicks instead of purchases or revenue.

  • Ignoring stock, price changes and seasonality.

  • Popularity feedback loops that bury new products.

Interview Tips

  • Clarify goals and placements before designing.

  • Draw the retrieval → ranking → re-ranking pipeline and discuss cold start and metrics.

41

How would you design a music recommendation engine?

A music recommendation engine combines collaborative signals from listening behaviour (plays, skips, saves, playlist additions), content signals from the audio and metadata (audio embeddings, tempo, genre, lyrics, artist), and context (time of day, activity, device, current session) in a retrieval-and-ranking pipeline that serves several products: personalized playlists, radio that continues from a seed track, “next track” in a session, and new-release discovery. Music has special traits: tracks are short and replayed often, skips are a strong fast negative signal, order and flow within a session matter, and the long tail of new and niche artists needs content-based cold start.

Read full answer

Applies togeneral system design (implicit feedback MF, sequence models, audio embeddings, bandits)

Signals

Signal Meaning
Full play, repeat play Strong positive
Save, add to playlist, follow artist Very strong positive
Skip within ~30 seconds Negative
Playlist co-occurrence Tracks that belong together (great for similarity)
Audio features Tempo, energy, timbre embeddings for cold start
Context Morning commute vs workout vs late night

Architecture

  1. Embeddings:

  2. Implicit-feedback matrix factorization (weighted ALS) on user × track play counts.

  3. word2vec-style track embeddings from playlist and session sequences.

  4. Audio embeddings from a CNN or transformer over spectrograms for new tracks.

  5. Candidate generation: nearest neighbours of the user’s taste vector and recent tracks; same-artist and similar-artist expansions; new releases from followed artists.

  6. Ranking: a model predicting “listen without skip” or “save” from user, track, artist and context features.

  7. Sequencing: order tracks for smooth flow (tempo and key transitions), avoid repeating the same artist back to back.

  8. Exploration: bandits give new and niche tracks a share of slots and test new genres.

  9. Feedback: skips update session state immediately (session-based sequence model such as a GRU or transformer).

Product Examples

Product Approach
Weekly discovery playlist Tracks close to taste vector, not yet heard, with diversity
Radio from a seed Neighbours of the seed in playlist-embedding space
Daily mixes Cluster the user’s taste into a few groups, one mix per cluster
New releases Followed artists + content similarity

Evaluation

  • Offline: next-track hit rate@k, NDCG, skip prediction AUC; diversity and artist coverage.

  • Online: listening time, skip rate, saves, return rate, discovery of new artists.

Common Pitfalls

  • Optimizing raw plays, which favours background and repeated tracks.

  • Popularity bias that ignores emerging artists.

  • Ignoring context: a workout playlist at bedtime.

  • Too much repetition of familiar songs, or too much novelty for a user who wants comfort.

Interview Tips

  • Mention skips as negative implicit feedback and playlists as a similarity source.

  • Discuss balancing familiarity and discovery.

42

How do you personalize recommendations for a video streaming service?

Personalizing a video streaming service means learning each member’s taste from viewing behaviour (what they start, how much they finish, rewatch, rate, search and abandon) and using it in a multi-stage system that generates candidates, ranks titles for each row of the home page, chooses which rows to show and in what order, and even picks personalized artwork, while handling household profiles, fresh releases and binge-watching sequences. The core objective is long-term satisfaction and retention, not just clicks, so watch completion and return visits weigh more than plays started.

Read full answer

Applies togeneral system design (two-tower retrieval, deep ranking, sequence models, contextual bandits)

Signals and Objectives

Signal Use
Completion rate, watch time Strong positive (normalized by title length)
Started then abandoned Negative or weak signal
Rewatch, binge (several episodes in a row) Very strong positive
Thumbs up/down, “My List” Explicit preference
Search queries Unmet intent; titles not in catalogue
Time, device, profile Context (kids’ profile, TV vs mobile)

System Design

  1. Candidate generation: two-tower model over user history and title metadata; “because you watched X” neighbours; trending in region; new releases; continue-watching.

  2. Ranking: a deep model predicting probability of meaningful viewing (e.g. completing a significant share) with user, title, context and freshness features.

  3. Page construction: choose rows (genres, “Top 10”, “Because you watched…”) and order them; rows and titles are both ranked, avoiding duplicates across rows.

  4. Artwork personalization: a contextual bandit picks the thumbnail most likely to appeal to this member.

  5. Sequence awareness: series continuation and next-episode autoplay; sequence models for session intent.

  6. Fresh content: exploration slots and content-based embeddings (synopsis, cast, genre, trailer) for new titles.

Handling Special Cases

  • Shared accounts: separate profiles; detect mixed tastes and diversify.

  • Kids’ profiles: strict maturity filters.

  • Regional catalogues and licensing: recommend only available titles.

  • Cold start: onboarding picks a few liked titles; popularity by region and device.

Evaluation

  • Offline: recall@k and NDCG on time-based splits, ranking of rows, calibration of genre mix.

  • Online A/B: hours viewed per member, retention and cancellation rate, share of members finding something to watch quickly; guardrails for diversity.

Common Pitfalls

  • Optimizing clicks on artwork that leads to abandoned plays (clickbait thumbnails).

  • Recommending titles the user already finished.

  • A filter bubble of one genre; calibrate to the user’s mix of interests.

Interview Tips

  • Mention row-level and page-level personalization, not just a single list.

  • Name long-term retention as the business metric and completion as the core signal.

43

How do you improve movie recommendations for users with diverse demographics?

Improving movie recommendations for a diverse user base means personalizing on individual behaviour rather than demographic stereotypes, building a catalogue representation that understands language, culture, region and content style, handling cold start with region- and language-aware popularity, evaluating quality separately for each user segment, and actively managing diversity, fairness and exposure so that minority groups and niche content aren’t underserved by a model trained mostly on the majority. Demographics should only inform defaults and evaluation, with consent, never lock users into what “people like them” watch.

Read full answer

Applies togeneral concepts (segment-aware evaluation, hybrid recommenders, fairness-aware re-ranking)

Strategy

  1. Understand the segments: language, region, age band (where lawful), device, viewing context; measure current quality per segment.

  2. Rich content representation: language, country, cast, themes, maturity rating, subtitles and dubbing availability, multilingual text embeddings of synopses.

  3. Behaviour-first personalization: collaborative models and sequence models learn individual tastes, which differ widely within any demographic group.

  4. Context and availability: recommend titles available in the user’s region and language (audio or subtitles).

  5. Cold start: onboarding taste picks; popularity within the user’s language and region; a mix of local and global titles.

  6. Calibration: match the genre and language mix of recommendations to the user’s history, with room for exploration.

  7. Diversity and fairness re-ranking: ensure local and minority-language content gets exposure; avoid feedback loops that only promote global blockbusters.

Evaluation by Segment

Check Why
Metrics (recall@k, NDCG) per language, region, age band and activity level Averages hide poor service for smaller groups
Catalogue coverage by content origin Detects popularity and majority bias
Calibration error Lists match each user’s interest mix
Online A/B results per segment A global win can be a loss for a minority group

Techniques

  • Multi-task or multi-domain models with shared and segment-specific parameters.

  • Balanced training data: reweight under-represented segments.

  • Exploration via bandits for local and niche titles.

  • User controls: preferred languages, “not interested”, maturity settings.

Responsible Use of Demographics

  • Collect only with consent and a clear purpose; comply with privacy laws.

  • Don’t infer sensitive attributes; use demographics for evaluation and defaults, not hard filters.

  • Audit for stereotyping (e.g. recommending only certain genres by gender).

Common Pitfalls

  • Using demographics as the main signal, which stereotypes users.

  • Reporting one global metric.

  • Ignoring language availability, so users get films they can’t understand.

Interview Tips

  • Lead with per-segment evaluation; it shows a data-driven approach.

  • Discuss calibration and fairness re-ranking as concrete fixes.

44

How would you design a recommendation system for an EdTech platform?

An EdTech recommender should optimize learning outcomes, not just engagement: it recommends the next lesson, exercise, course or resource based on the learner’s goals, current knowledge (estimated from quiz and exercise results), prerequisites between topics, difficulty, and learning behaviour, combining knowledge tracing models with collaborative and content-based signals and educational constraints. Good recommendations keep learners in a zone of appropriate challenge, fill knowledge gaps, schedule review with spaced repetition, and are judged by mastery, completion and retention rather than clicks.

Read full answer

Applies togeneral system design (knowledge tracing, item response theory, hybrid recommenders, spaced repetition)

What to Recommend

Recommendation Approach
Next lesson or exercise Knowledge tracing + prerequisite graph
Review items Spaced repetition scheduling (forgetting curve)
Courses or learning paths Goals + collaborative filtering on enrolments + content similarity
Resources (videos, articles) Content-based on topic embeddings
Study peers or groups Similar goals and level

Key Components

  • Learner model: estimated mastery per skill, using Bayesian Knowledge Tracing, deep knowledge tracing (sequence models) or item response theory.

  • Content model: skills tagged on every item, difficulty estimates, prerequisite graph, formats and duration.

  • Difficulty targeting: pick items with a predicted success probability of roughly 60-80%, challenging but achievable.

  • Prerequisites: never recommend content whose prerequisites aren’t mastered.

  • Collaborative signals: learners with similar paths succeeded after this resource.

  • Context: available time, device, exam date, preferred format.

Pipeline

  1. Learner goal and diagnostic assessment (cold start).

  2. Candidate items: next skills in the prerequisite graph, weak skills, due review items, popular items for the goal.

  3. Ranking by expected learning gain, difficulty fit and engagement probability.

  4. Re-ranking for variety of formats and session length.

  5. Feedback: answers update mastery estimates immediately.

Evaluation

  • Learning outcomes: mastery gains, assessment scores, retention after weeks.

  • Engagement: completion and return rates, as secondary metrics.

  • A/B tests with learning-outcome primary metrics; check fairness across learner groups.

Ethical and Practical Concerns

  • Privacy of minors: strict consent and data protection (e.g. COPPA in the US, GDPR).

  • Equity: don’t steer learners from some groups to easier tracks.

  • Transparency: explain why something is recommended (“you missed 3 of 5 questions on fractions”).

  • Teacher control: let educators override or assign content.

Common Pitfalls

  • Optimizing time on platform, which can reward easy, entertaining content.

  • Ignoring prerequisites, so learners get content they aren’t ready for.

  • Treating all wrong answers equally (careless vs conceptual errors).

Interview Tips

  • Stress learning outcomes as the objective and knowledge tracing as the learner model.

  • Mention spaced repetition and difficulty targeting.

45

How would you build a "people you may know" connection recommender?

A “people you may know” recommender suggests new connections mainly from the social graph: it generates candidates who are close in the graph (friends of friends, shared groups, schools, workplaces, contacts imported with consent), scores them with features such as the number of mutual connections, graph proximity and profile similarity, ranks them with a model trained to predict a sent and accepted connection request, and filters out people who shouldn’t be suggested for privacy or safety reasons. It is a link-prediction problem on a large graph, and the most important design concerns besides accuracy are privacy, reciprocity (both sides should welcome the connection) and avoiding unwanted exposure.

Read full answer

Applies togeneral system design (graph link prediction, graph embeddings, learning-to-rank)

Candidate Generation

Source Note
Friends of friends (2-hop) Strongest signal; count of mutual friends
Shared affiliations Same school, employer, city, groups, events
Contact upload (opt-in) Matching hashed phone numbers or emails
Graph embeddings node2vec, GraphSAGE; ANN search over user vectors
Interaction signals Profile views, comments on the same posts

Ranking Features

  • Graph: mutual connections, Adamic-Adar, Jaccard of neighbourhoods, shortest path length, personalized PageRank score.

  • Profile: shared school or company, location, industry, language.

  • Activity: both users active recently, likely to accept.

  • Reciprocity: predicted probability that the other person accepts.

Label: request sent and accepted; train a GBM or GNN-based model; optimize for accepted connections, not clicks.

Code Example

import math

graph = {
    "ana": {"ben", "cai", "dee"},
    "ben": {"ana", "cai", "eve"},
    "cai": {"ana", "ben", "eve", "fay"},
    "dee": {"ana"},
    "eve": {"ben", "cai"},
    "fay": {"cai"},
}

def suggestions(user, n=3):
    scores = {}
    for friend in graph[user]:
        for candidate in graph[friend]:
            if candidate == user or candidate in graph[user]:
                continue
            # Adamic-Adar: mutual friends with few connections count more
            scores[candidate] = scores.get(candidate, 0.0) + 1 / math.log(len(graph[friend]))
    return sorted(((c, round(s, 3)) for c, s in scores.items()), key=lambda x: -x[1])[:n]

print(suggestions("ana"))   # eve (2 mutual friends) ranks above fay (1)

Privacy and Safety

  • Don’t reveal sensitive links (for example therapist-patient connections inferred from contacts or location).

  • Respect blocks, “don’t suggest” choices and privacy settings; let users remove imported contacts.

  • Avoid suggesting minors to unknown adults.

  • Be careful with location-based suggestions, which can expose people.

Evaluation

  • Offline: precision@k and recall@k of future connections on a time-based split of the graph.

  • Online: accepted connections per user, acceptance rate, network growth, plus guardrails such as “remove suggestion” and report rates.

Common Pitfalls

  • Optimizing requests sent rather than accepted, which spams recipients.

  • Leaking private relationships through suggestions.

  • Only suggesting highly connected users, which strengthens popularity bias.

Interview Tips

  • Frame it as link prediction with friends-of-friends candidates.

  • Raise privacy and reciprocity early.

46

What are the ethical issues with recommendation systems and how can they be addressed?

The main ethical issues with recommendation systems are filter bubbles and polarization, amplification of harmful, misleading or extreme content, manipulation and addictive design that optimize engagement over well-being, unfair exposure and discrimination against groups of users or creators, privacy invasion from behavioural profiling, and a lack of transparency and user control. They are addressed by choosing better objectives (long-term satisfaction rather than raw engagement), adding diversity and exposure fairness, moderating harmful content, protecting privacy, explaining recommendations and giving users control, and auditing outcomes by group, alongside legal requirements such as the EU Digital Services Act.

Read full answer

Applies togeneral concepts (responsible recommender design; EU Digital Services Act, GDPR)

Issues and Mitigations

Issue Example Mitigations
Filter bubbles / echo chambers Users see only one viewpoint Diversity re-ranking, calibration, exploration
Harmful amplification Extreme or false content spreads because it engages Content policies, quality and integrity signals in ranking, down-ranking borderline content
Engagement over well-being Endless feeds, late-night autoplay Optimize satisfaction surveys and long-term retention; time-well-spent features
Unfair exposure New or minority creators rarely shown Exposure fairness constraints, audits of exposure by group
Discrimination Job or housing ads shown differently by gender or ethnicity Remove or test proxies, fairness audits, legal review
Privacy Inferring health or sexuality from behaviour Data minimization, consent, differential privacy
Manipulation Dark patterns, hidden sponsored placement Clear labelling of sponsored items, no deceptive design
Opacity Users don’t know why they see something “Why am I seeing this”, user controls, non-personalized option
Vulnerable users Minors, people with addictions Age-appropriate settings, stronger safeguards

Practices

  • Objectives: include long-term and well-being metrics, not only clicks or watch time.

  • Measurement: monitor diversity, exposure distribution, harmful-content prevalence and outcomes per user group.

  • Human oversight: policy teams, red-teaming and impact assessments before launches.

  • User agency: “not interested”, topic controls, history deletion, chronological feeds.

  • Transparency: explain main ranking parameters; the EU Digital Services Act requires very large platforms to offer at least one recommender option not based on profiling and to assess systemic risks.

  • Feedback loop awareness: exploration and debiasing so the system doesn’t only reinforce what it already showed.

Common Pitfalls

  • Treating ethics as a post-launch patch instead of a design input.

  • Measuring fairness only for users and ignoring creators (or the other way round).

  • Assuming that removing a sensitive attribute removes bias; proxies remain.

Interview Tips

  • Name concrete metrics (exposure share, diversity, harmful prevalence) to show ethics can be measured.

  • Mention multi-stakeholder fairness: users, creators and the platform.

47

What are state-of-the-art recommendation models such as Neural Collaborative Filtering?

State-of-the-art recommendation models are neural architectures that learn user and item representations and their interactions from large, rich data: Neural Collaborative Filtering (NCF) replaces the matrix-factorization dot product with a learned neural function, two-tower models power large-scale retrieval, deep cross-feature models (Wide & Deep, DeepFM, DCN-V2, DLRM) dominate ranking, transformer sequence models (SASRec, BERT4Rec) model user histories, and graph neural networks (PinSage, LightGCN) exploit the user-item graph. Recent work adds generative retrieval with semantic item IDs and large-language-model recommenders; in practice, well-tuned simpler baselines remain competitive, so models are chosen per stage and data size.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with PyTorch 2.x

Neural Collaborative Filtering (He et al., 2017)

  • GMF: element-wise product of user and item embeddings (generalizes MF).

  • MLP: concatenated embeddings through dense layers to learn non-linear interactions.

  • NeuMF: combines GMF and MLP outputs into a final prediction; trained on implicit feedback with sampled negatives and binary cross-entropy.

  • Later studies (Rendle et al., 2020) showed a well-tuned dot product often matches or beats the MLP interaction, an important caveat.

Other Model Families

Model Idea Stage
Two-tower / dual encoder Separate user and item networks, dot product, ANN search Retrieval
Wide & Deep Memorization (wide cross features) + generalization (deep) Ranking
DeepFM, DCN-V2 Learn feature crosses automatically Ranking (CTR)
DLRM Embedding tables + MLPs, built for scale Ranking
SASRec, BERT4Rec Self-attention over the interaction sequence Next-item
LightGCN, PinSage Graph convolutions over user-item graph Retrieval, embeddings
Multi-task (MMoE, PLE) Shared experts for clicks, watch time, likes Ranking
Generative retrieval / LLM-based Predict semantic IDs of items; LLMs for cold start and explanations Emerging

Code Example

import torch
import torch.nn as nn

torch.manual_seed(42)

class NeuMF(nn.Module):
    def __init__(self, n_users, n_items, dim=16):
        super().__init__()
        self.u_gmf, self.i_gmf = nn.Embedding(n_users, dim), nn.Embedding(n_items, dim)
        self.u_mlp, self.i_mlp = nn.Embedding(n_users, dim), nn.Embedding(n_items, dim)
        self.mlp = nn.Sequential(nn.Linear(2 * dim, 32), nn.ReLU(), nn.Linear(32, 16), nn.ReLU())
        self.out = nn.Linear(dim + 16, 1)

    def forward(self, u, i):
        gmf = self.u_gmf(u) * self.i_gmf(i)
        mlp = self.mlp(torch.cat([self.u_mlp(u), self.i_mlp(i)], dim=-1))
        return self.out(torch.cat([gmf, mlp], dim=-1)).squeeze(-1)   # logit

model = NeuMF(n_users=100, n_items=200)
users = torch.randint(0, 100, (256,))
items = torch.randint(0, 200, (256,))
labels = torch.randint(0, 2, (256,)).float()      # 1 = interaction, 0 = sampled negative
loss = nn.functional.binary_cross_entropy_with_logits(model(users, items), labels)
loss.backward()
print("loss:", round(loss.item(), 3))

Common Pitfalls

  • Comparing a new model with untuned baselines; tune MF and item-kNN properly.

  • Sampled evaluation metrics that change model rankings.

  • Ignoring serving cost: MLP interactions can’t use ANN retrieval directly.

Interview Tips

  • Explain why retrieval uses dot-product two-tower models and ranking uses feature-cross models.

  • Mention the NCF vs dot-product debate to show critical understanding.

48

How does context-aware recommendation work and when is it most useful?

Context-aware recommendation adds information about the situation of the interaction, such as time, location, device, weather, companions, activity or the current session, to the user and item signals, so the system predicts what a user wants in this context rather than in general. It is modelled by filtering data or results by context (pre- or post-filtering) or by putting context directly into the model (contextual modelling with factorization machines, deep rankers or contextual bandits), and it pays off most where needs change strongly with situation: food delivery, travel, music, news, mobile and local services.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Types of Context

Context Example
Time Breakfast vs dinner, weekday vs weekend, season, holidays
Location City, home vs travelling, near a store
Device Mobile in a commute vs TV in the evening
Social Alone vs with family or kids
Activity / intent Workout, studying, gift shopping
Session Items viewed in the last few minutes
Environment Weather, local events

Approaches (Adomavicius and Tuzhilin)

  • Contextual pre-filtering: train or score using only data from the matching context (e.g. weekend ratings).

  • Contextual post-filtering: produce normal recommendations, then filter or re-weight them by context.

  • Contextual modelling: include context in the model: tensor factorization, factorization machines (user × item × context interactions), context features in deep rankers, or contextual bandits.

Code Example

import numpy as np

items = ["coffee", "salad", "pizza", "ice cream"]
base_score = np.array([0.6, 0.5, 0.7, 0.4])           # user's general preference
time_boost = {                                         # learned context effects
    "morning": np.array([0.4, 0.0, -0.3, -0.2]),
    "evening": np.array([-0.3, 0.0, 0.3, 0.1]),
}
hot_day_boost = np.array([0.0, 0.1, 0.0, 0.3])

def recommend(time_of_day, hot_day, n=2):
    score = base_score + time_boost[time_of_day] + (hot_day_boost if hot_day else 0)
    return [items[i] for i in np.argsort(-score, kind="stable")[:n]]

print(recommend("morning", hot_day=False))   # ['coffee', 'salad']
print(recommend("evening", hot_day=True))    # ['pizza', 'ice cream']

When It Helps Most

  • Strongly situational needs: restaurants, food delivery, ride-hailing, travel.

  • Mobile and location-based services: nearby offers, points of interest.

  • Music and video: mood and activity, device and time of day.

  • News: time sensitivity and trending events.

  • Session-based shopping: current intent differs from long-term taste.

Challenges

  • Context data is sparse and multiplies the space (users × items × contexts).

  • Some context is unobserved (mood, companions) and must be inferred.

  • Location and activity context are privacy-sensitive and need consent.

Common Pitfalls

  • Over-splitting data by context so each slice is too small (pre-filtering).

  • Context features available offline but not at serving time.

  • Assuming context always helps; measure the gain per context type.

Interview Tips

  • Name the three paradigms: pre-filtering, post-filtering and contextual modelling.

  • Give a concrete domain where context dominates, such as food delivery.

49

What role do multi-armed bandits play in recommendation systems?

Multi-armed bandit algorithms decide, one request at a time, which item or variant to show while balancing exploitation (showing what currently looks best) and exploration (trying uncertain options to learn about them), so they learn from live feedback with less wasted traffic than a fixed A/B test. In recommenders they are used for cold-start exploration of new items, choosing among a few candidates or layouts (headlines, thumbnails, rows), adapting quickly to trends, and, as contextual bandits such as LinUCB, personalizing choices with user and context features.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Common Algorithms

Algorithm Idea Note
Epsilon-greedy Explore at random with probability ε Simple; wastes exploration on clearly bad arms
UCB1 Pick the highest mean + confidence bonus Optimism under uncertainty
Thompson sampling Sample from each arm’s posterior, pick the best sample Strong in practice; Beta-Bernoulli for clicks
LinUCB / linear Thompson Context features predict reward Personalized (contextual bandit)
Neural bandits Neural reward model with uncertainty Rich features

Uses in Recommendation

  • New item cold start: give new items exposure and learn their appeal quickly.

  • Creative optimization: thumbnails, titles, push notification texts.

  • Page and row selection: which modules to show.

  • Ranking exploration: occasionally promote uncertain items to collect unbiased data.

  • Fast-changing catalogues: news and trending content where historical data is stale.

Code Example

import numpy as np

rng = np.random.default_rng(42)
true_ctr = np.array([0.04, 0.06, 0.10])        # unknown to the algorithm
n_arms, rounds = 3, 10_000
counts, values = np.zeros(n_arms), np.zeros(n_arms)

for t in range(1, rounds + 1):                 # UCB1
    if t <= n_arms:
        arm = t - 1                            # play each arm once
    else:
        ucb = values + np.sqrt(2 * np.log(t) / counts)
        arm = int(np.argmax(ucb))
    reward = float(rng.random() < true_ctr[arm])
    counts[arm] += 1
    values[arm] += (reward - values[arm]) / counts[arm]   # running mean

print("pulls per arm:", counts.astype(int).tolist())       # most pulls go to arm 2
print("estimated CTR:", np.round(values, 3).tolist())

Bandits vs A/B Tests

Aspect A/B test Bandit
Goal Clean causal estimate Maximize reward while learning
Traffic split Fixed Adaptive
Regret Higher (loser keeps traffic) Lower
Best for Big launches, long-term metrics Many short-lived options, cold start

Common Pitfalls

  • Using clicks as reward, which favours clickbait; use satisfaction signals.

  • Non-stationary rewards: use sliding windows or discounting.

  • Delayed feedback (purchases) and position bias complicate rewards.

  • Logging without propensities, which blocks later off-policy evaluation.

Interview Tips

  • Explain the exploration-exploitation trade-off and regret.

  • Mention contextual bandits for personalization and Thompson sampling as a strong default.

50

What are graph-based recommendation systems and their advantages?

Graph-based recommendation systems represent users, items and other entities (categories, tags, brands, creators) as nodes connected by edges for interactions and relationships, and recommend by exploiting the graph structure: random walks and personalized PageRank, link prediction, graph embeddings such as node2vec, or graph neural networks such as PinSage and LightGCN that pass information along edges to learn representations. Their advantages are capturing multi-hop, higher-order relationships (users who like items liked by similar users), naturally combining heterogeneous data such as social links and knowledge graphs, helping sparse and cold-start nodes through their neighbours, and giving path-based explanations.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x

Graph Types

Graph Nodes and edges Use
Bipartite user-item Users–items (interactions) Collaborative filtering
Social graph Users–users Friend-based recommendations
Knowledge graph Items–attributes–entities Content and explanation (e.g. film → director)
Session graph Items with transitions Session-based next item
Heterogeneous graph All of the above Rich hybrid models

Methods

  • Random walks / personalized PageRank: walk from the user’s node and rank items by visit probability (Pinterest’s Pixie).

  • Graph embeddings: DeepWalk, node2vec learn vectors from random-walk sequences.

  • Graph neural networks: GraphSAGE and PinSage (sampled neighbourhoods, web scale), LightGCN (simplified propagation for CF).

  • Knowledge-graph models: KGAT, RippleNet use attribute paths.

Code Example

import numpy as np

# Bipartite graph: 3 users x 4 items (1 = interaction)
A = np.array([[1, 1, 0, 0],
              [0, 1, 1, 0],
              [0, 0, 1, 1]], dtype=float)
n_u, n_i = A.shape
adj = np.block([[np.zeros((n_u, n_u)), A], [A.T, np.zeros((n_i, n_i))]])
P = adj / adj.sum(axis=1, keepdims=True)          # random-walk transition matrix

def personalized_pagerank(user, alpha=0.15, steps=50):
    restart = np.zeros(n_u + n_i)
    restart[user] = 1.0
    r = restart.copy()
    for _ in range(steps):
        r = alpha * restart + (1 - alpha) * r @ P  # walk, or jump back to the user
    return r[n_u:]                                 # scores of item nodes

scores = personalized_pagerank(0)
scores[A[0] > 0] = -np.inf                         # hide items user 0 already has
print("recommend item:", int(np.argmax(scores)))   # 2: two hops away via item 1 and user 1

Advantages and Limitations

  • Advantages: multi-hop signals, heterogeneous data, cold start through connected attributes or friends, explainable paths, strong results with LightGCN-style models.

  • Limitations: large graphs are expensive (neighbourhood sampling, distributed training needed), popular hub nodes dominate walks, dynamic graphs need frequent updates, and GNNs can over-smooth with many layers.

Common Pitfalls

  • Letting very popular items absorb most random-walk mass; normalize by degree.

  • Leaking future edges into training graphs.

  • Too many GNN layers (over-smoothing).

Interview Tips

  • Mention PinSage or Pixie as web-scale examples and LightGCN for CF.

  • Explain multi-hop reasoning as the key advantage over plain matrix factorization.

Sign in to keep reading

You've opened 2 full answers. Sign in to read all 50 Recommendation Systems answers, mark what you know and pick up where you left off.

Report a mistake

What's wrong?

All Machine Learning & Data Science subjects