A practice prompt we wrote. No company or candidate report names it, so it carries no company tag.
How to answer
Three things are checked: the formula, what each parameter does, and a comparison that is a measurement rather than an impression. Take them in that order.
-
Write the formula before the code. Check one score by hand on a three-document corpus before you trust the rest.
score(D, Q) = sum over t in Q of idf(t) * tf * (k1 + 1) / (tf + k1 * (1 - b + b * len(D) / avgdl)) idf(t) = ln(1 + (N - df + 0.5) / (df + 0.5))Say why that IDF: Lucene uses it, and unlike the original it never goes negative for a very common term (Lucene BM25Similarity).
-
Explain the knobs out loud.
k1sets how fast repeats stop adding score: at zero only presence counts.bsets how much length is penalized: zero ignores it, one normalizes fully (Manning, Raghavan and Schütze, section 11.4.3). Lucene’s defaults,k1=1.2andb=0.75, are a starting point to tune on labeled queries. -
Share everything except the scorer. Same tokenizer, chunks and index for both. Say which TF-IDF you mean: raw counts, or sublinear with cosine, which already damps repetition and length.
-
Compare on labeled queries. Report recall at
kand MRR for both, then show where they disagree. The telling case isrefund policy: one long page says “refund” a dozen times, another says “refund” and “policy” once each. Raw-count TF-IDF can rank the first higher; BM25’s saturation (k1) and length normalization (b) together can put the page matching both words above it. -
Name what neither fixes. Synonyms and paraphrase need embeddings.
The trap is eyeballing two result lists and declaring a winner. Before you compare anything, write down a handful of queries with the documents that should come back for each, so the comparison has something to score against.
Follow-ups
What the interviewer may ask next, once your first answer is on the table.
- The query repeats a word, like
python python tutorial. Does your code count it twice, and should it? - New documents arrive every day. What in your index goes stale, and what do you recompute?
- BM25 wins on your labeled queries but users still complain. What kind of query is it failing on?
Where answers go wrong
- Writes the formula from memory with the original IDF, which goes negative for a very common term, so matching a common query word lowers the score.
- Compares the two scorers by reading a few result lists, with no labeled queries and no metric.
Answer this in two minutes
Write the answer you would say out loud. The clock starts with your first word.
Model answer
“The formula first, then one score by hand, then code. For a query Q and a document D, BM25 sums over the query’s terms:” “tf is the term’s count in the document, N the number of documents, df how many contain the term, and avgdl the average document length.