Embeddings for ML system design interviews 9 questions in 4 chapters
- 01What an embedding should capture
- 02Training embeddings3 questions
- 03Using, evaluating and serving embeddings4 questions
- 04Sources
What an embedding should capture
Why dense vectors replace one-hot IDs, and why the training objective defines similarity.
1.1 Why use embeddings instead of one-hot IDs, and what decides what "close" means? Medium 2 min
A one-hot ID gives each item its own weight, so nothing learned about one item helps a similar one, and new items have no slot. An embedding is a short dense vector learned so that items that behave alike sit close together. What close means comes entirely from the training objective: bought together suits upsell, while similar descriptions suit search.
Read the full answer1.2 How does matrix factorization learn item embeddings, and why start with it? Medium 2 min
Put past orders in a cart-by-product matrix and learn two thin matrices whose row dot products reconstruct it. Purchases pull a cart and its products together, while empty cells push weakly, because not buying is weak evidence. The product rows become item vectors. It is cheap and well understood, so it is a strong baseline that proves later complexity is worth it.
Read the full answer
Training embeddings
Two-tower models, contrastive losses, negatives, graphs and pre-trained encoders.
2.1 How do you train a two-tower retrieval model with in-batch and hard negatives? Hard 4 min
One tower encodes the query side, such as the cart, and one encodes candidate products into the same space, so their dot product is the score. I hold one item out of each completed order as the positive, use other items in the batch as negatives, and train with InfoNCE. I add periodically mined hard negatives, correct for popularity bias and split evaluation by time.
Read the full answer2.2 When do graph embeddings help, and should you pick node2vec or GraphSAGE? Hard 3 min
A graph helps when links such as product to brand, category or promoter carry signal that carts miss. node2vec turns random walks into sentences and trains a word2vec-style objective, but learns one vector per node, so new nodes wait for retraining. GraphSAGE learns a function over a node's features and sampled neighbours, so a new product gets a vector immediately. Always justify the graph first.
Read the full answer2.3 Should you train your own text encoder, fine-tune one, or call an embedding API? Medium 2 min
A store catalog is far too small to pre-train a large language encoder, so I start from a pre-trained one. I size it to where it runs: catalog items are embedded in batch, so a larger model is affordable, while queries are embedded per request, so latency pushes toward smaller. I fine-tune contrastively on my own pairs, or use LoRA adapters, and weigh hosted APIs against cost and deprecation risk.
Read the full answer
Using, evaluating and serving embeddings
Features, clustering, retrieval and RAG; evaluation; size, versions, freshness and cold start.
3.1 How are embeddings used downstream: features, clustering and retrieval? Medium 2 min
Embeddings are used in three ways. As features, they replace one-hot IDs and transfer knowledge to other models. For clustering, items close together form segments or near-duplicate groups. For retrieval, I embed candidates offline into an approximate nearest-neighbour index, embed the query online, and pass the neighbours to a heavier ranker. Text search usually runs hybrid with keywords, and RAG puts retrieved documents into a prompt.
Read the full answer3.2 How do you know an embedding is any good? Medium 1 min
I check intrinsic health first: a histogram of similarities on held-out data quickly shows a collapsed space where everything looks similar. Then I measure where the embedding is used. For retrieval that is recall at k on held-out, time-split orders, confirmed by an online test of the product metric. For a feature, I run an ablation: train the downstream model with and without it on the same data.
Read the full answer3.3 How do you choose embedding size, retrain safely and keep vectors fresh? Hard 2 min
More dimensions capture more nuance but cost more to train, store and search, so I size by whether serving cost or quality dominates. Matryoshka training lets one vector's prefixes work alone, so a short prefix retrieves and the full vector reranks. Retraining creates an incompatible space, so I re-embed the corpus into a new index and switch atomically. Freshness needs batch re-embedding plus event-driven updates.
Read the full answer3.4 How does a new item get useful embeddings before it has any behaviour? Hard 2 min
Content features such as title, category and price give a new product a vector on day one, but behaviour usually predicts purchases better once it exists. I give the model an input such as item age or purchase count, so it can rely on content when history is thin and on behaviour as it grows. Training data must contain items at every age, which feature logging provides.
Read the full answer
Sources
Source: Mikolov et al., Efficient Estimation of Word Representations in Vector Space Source: Mikolov et al., Distributed Representations of Words and Phrases and their Compositionality Source: Hu, Koren and Volinsky, Collaborative Filtering for Implicit Feedback Datasets Source: van den Oord et al., Representation Learning with Contrastive Predictive Coding Source: Yi et al., Sampling-Bias-Corrected Neural Modeling for Large Corpus Item Recommendations Source: Karpukhin et al., Dense Passage Retrieval for Open-Domain Question Answering Source: Xiong et al., Approximate Nearest Neighbor Negative Contrastive Learning for Dense Text Retrieval Source: Qu et al., RocketQA Source: Schroff et al., FaceNet Source: Reimers and Gurevych, Sentence-BERT Source: Perozzi et al., DeepWalk Source: Grover and Leskovec, node2vec Source: Hamilton et al., GraphSAGE Source: Hu et al., LoRA Source: Kusupati et al., Matryoshka Representation Learning Source: Malkov and Yashunin, HNSW Source: Gao et al., HyDE Source: Muennighoff et al., MTEB Source: Liu et al., Monolith Source: Sentence Transformers docs: losses Source: Faiss wiki: index types Source: pgvector README Source: OpenAI API docs: Deprecations