Coding questions · Apple
Implement TF-IDF scoring for documents
This question evaluates understanding of information retrieval and text representation concepts—specifically TF–IDF weighting, tokenization, term frequency and document frequency—and measures competency in implementing an efficient scoring and ranking algorithm.
Problem
Implement a simplified TF–IDF scorer.
You are given:
- A list of documents
docs, where each document is a string. - A query string
q.
Tokenization rules (for this problem):
- Lowercase everything.
- Split on non-letters/digits (you may assume a helper tokenizer is available or implement a simple one).
- Ignore empty tokens.
Definitions
- Term frequency:
- Document frequency:
- Inverse document frequency (smoothed):
where .
- Document score for query:
Task
Return the document indices sorted by descending score(d, q). Break ties by smaller index.
Constraints
1 <= len(docs) <= 10^4- Total tokens across all documents up to ~
2 * 10^6
Example
If docs = ["red apple", "green apple", "banana"] and q = "apple", documents 0 and 1 should rank above 2.
Implement TF-IDF Scoring for Documents
Implement a simplified TF-IDF scorer.
You are given a list of documents docs, where each document is a string, and a query string q.
Tokenization rules:
- Convert text to lowercase.
- Split on any non-letter/non-digit character.
- Ignore empty tokens.
Definitions:
tf(t, d)= number of times tokentappears in documentddf(t)= number of documents that contain tokentat least onceidf(t) = log((N + 1) / (df(t) + 1)) + 1, whereNis the number of documentsscore(d, q)= sum oftf(t, d) * idf(t)over all tokens in the query
Important: if a query token appears multiple times, it contributes multiple times to the sum.
Return a list containing every document index exactly once, sorted by descending score. If two documents have the same score, the smaller index comes first.
Use the natural logarithm.
Hints
- You only need document frequencies for terms that actually appear in the query.
- To save memory, compute document frequencies in one pass, then compute scores in a second pass once IDF values are known.