Design a resume search engine with an inverted index and TF-IDF ranking
Quick Overview
Design and implement a resume search engine that indexes documents, tokenizes text with stopword removal, and answers multi-word queries that must match every term, ranked by summed TF-IDF scores. It tests inverted index design, posting list intersection, exact scoring and tie rules, and edge cases such as repeated or unknown terms.
Design a resume search engine with an inverted index and TF-IDF ranking
Company: MongoDB
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a resume search system that lets recruiters search a collection of resumes with multi-word queries. The system should support adding documents, tokenizing text into searchable terms, maintaining an inverted index, and returning search results ranked by TF-IDF relevance.
When a multi-word query is issued, only documents containing all query terms are returned. Results are ranked by the sum of TF-IDF scores across the distinct query terms (a word repeated in the query counts once). TF (term frequency) is the count of a term in a document divided by the total number of tokens in that document after stopword removal. IDF (inverse document frequency) is `log(N / df)` with the natural logarithm, where `N` is the total number of documents stored and `df` is the number of documents containing the term.
Tokenization converts text to lowercase, splits on non-alphanumeric characters, and removes stopwords. Use this stopword list:
```text
a, an, the, is, are, was, were, be, been, being, have, has, had, do, does, did,
will, would, could, should, may, might, shall, can, to, of, in, for, on, with,
at, by, from, and, or, not, but, if, then, so, as, it, its, this, that, these, those
```
Queries go through the same tokenizer. Scores are rounded to 4 decimal places, and documents with equal rounded scores are ordered by document ID ascending.
Implement a class `SearchEngine` with these methods:
- `addDocument(docId: int, content: str)` adds a resume with the given ID and text. It tokenizes the content, removes stopwords, and updates the inverted index.
- `search(query: str) -> List[List]` returns the resumes that contain every term of the query as `[docId, score]` pairs, sorted by TF-IDF score descending, then docId ascending. It returns an empty list when no terms are left after tokenization or no resume matches.
- `getDocument(docId: int) -> str` returns the content of the document with the given ID, or an empty string if it is not stored.
- `getDocumentCount() -> int` returns the number of documents currently stored.
The first sequence adds three resumes and runs three searches:
```text
addDocument(1, "Senior Python developer with Django experience")
addDocument(2, "Java developer and Python scripting")
addDocument(3, "Data scientist using Python and SQL")
search("python developer") -> [[2, 0.1014], [1, 0.0811]]
search("Python") -> [[1, 0.0], [2, 0.0], [3, 0.0]]
search("the SQL") -> [[3, 0.2197]]
getDocumentCount() -> 3
getDocument(4) -> ""
```
After stopword removal the three resumes have 5, 4 and 5 tokens. "python" appears in all three, so its IDF is $\ln(3/3) = 0$ and the second search returns every resume with score 0, ordered by ID. "developer" appears in resumes 1 and 2, so its IDF is $\ln 1.5 \approx 0.4055$. Resume 2 scores $\tfrac{1}{4} \cdot 0.4055 \approx 0.1014$ and resume 1 scores $\tfrac{1}{5} \cdot 0.4055 \approx 0.0811$. "the" is a stopword, so the third search looks for "sql" alone, which only resume 3 contains: $\tfrac{1}{5} \ln 3 \approx 0.2197$.
The second sequence shows a repeated word inside a resume and a tie:
```text
addDocument(20, "Kubernetes Go")
addDocument(10, "Go Go Kubernetes")
addDocument(30, "Rust")
search("go") -> [[10, 0.2703], [20, 0.2027]]
search("kubernetes go") -> [[10, 0.4055], [20, 0.4055]]
search("go rust") -> []
```
"go" is in two of the three resumes, so its IDF is $\ln 1.5$. Resume 10 has it in 2 of its 3 tokens ($\tfrac{2}{3} \cdot 0.4055 \approx 0.2703$) and resume 20 in 1 of its 2 tokens ($\approx 0.2027$). For "kubernetes go" both resumes score $\ln 1.5 \approx 0.4055$, so the tie goes to the lower ID, 10, even though resume 20 was added first. No resume contains both "go" and "rust", so the last search is empty.
```hint What to store and when
A search needs, for each term, the resumes that contain it, and for each resume, how often each term appears and how many tokens it has. Decide where each of those lives and update all of them when a document is added.
```
```hint Filtering before scoring
Narrow the resumes down to those containing every query term before you compute any score. Which term's list of resumes is the cheapest place to start?
```
### Constraints & Assumptions
- `1 <= docId <= 10^5`
- Document content is a non-empty string of alphanumeric characters and spaces.
- The query is a non-empty string.
- At most `10^4` calls to `addDocument` and `search` combined.
- Scores are rounded to 4 decimal places.
### Clarifying Questions to Ask
- What should `addDocument` do with a docId that is already stored: replace the old resume, ignore the call, or raise an error?
- Does `N` count a resume whose text is made entirely of stopwords?
- Will resumes ever be edited or removed, so that the index has to support deletes?
- Should `search` return every match, or only the top few results?
### What a Strong Answer Covers
- A single tokenizer that applies lowercasing, splitting and stopword removal the same way to resumes and queries.
- The data structures for the document store, per-resume term counts and lengths, and the inverted index, and how `addDocument` keeps them consistent.
- How `search` restricts the candidates to resumes containing every query term, and what that step costs.
- The exact scoring arithmetic: repeated query words, terms that appear in every resume, rounding, and the tie rule.
- Edge cases such as a query that tokenizes to nothing, unknown terms, a re-added docId, and resumes with no indexable tokens.
### Follow-up Questions
- How would you support removing a resume, and what happens to `N`, `df` and the scores of other resumes when you do?
- The collection grows to millions of resumes and recruiters only look at the top 10 results. What would you change in the intersection and ranking steps?
- How would you switch the ranking to BM25, and what extra state would the index need?
- How would you make the index safe when `addDocument` and `search` run concurrently?
Overview: Design and implement a resume search engine that indexes documents, tokenizes text with stopword removal, and answers multi-word queries that must match every term, ranked by summed TF-IDF scores. It tests inverted index design, posting list intersection, exact scoring and tie rules, and edge cases such as repeated or unknown terms.
Design a resume search system that lets recruiters search a collection of resumes with multi-word queries. The system should support adding documents, tokenizing text into searchable terms, maintaining an inverted index, and returning search results ranked by TF-IDF relevance.
When a multi-word query is issued, only documents containing all query terms are returned. Results are ranked by the sum of TF-IDF scores across the distinct query terms (a word repeated in the query counts once). TF (term frequency) is the count of a term in a document divided by the total number of tokens in that document after stopword removal. IDF (inverse document frequency) is log(N / df) with the natural logarithm, where N is the total number of documents stored and df is the number of documents containing the term.
Tokenization converts text to lowercase, splits on non-alphanumeric characters, and removes stopwords. Use this stopword list:
a, an, the, is, are, was, were, be, been, being, have, has, had, do, does, did,
will, would, could, should, may, might, shall, can, to, of, in, for, on, with,
at, by, from, and, or, not, but, if, then, so, as, it, its, this, that, these, those
Queries go through the same tokenizer. Scores are rounded to 4 decimal places, and documents with equal rounded scores are ordered by document ID ascending.
Implement a class SearchEngine with these methods:
addDocument(docId: int, content: str)
adds a resume with the given ID and text. It tokenizes the content, removes stopwords, and updates the inverted index.
search(query: str) -> List[List]
returns the resumes that contain every term of the query as
[docId, score]
pairs, sorted by TF-IDF score descending, then docId ascending. It returns an empty list when no terms are left after tokenization or no resume matches.
getDocument(docId: int) -> str
returns the content of the document with the given ID, or an empty string if it is not stored.
getDocumentCount() -> int
returns the number of documents currently stored.
The first sequence adds three resumes and runs three searches:
After stopword removal the three resumes have 5, 4 and 5 tokens. "python" appears in all three, so its IDF is ln(3/3)=0 and the second search returns every resume with score 0, ordered by ID. "developer" appears in resumes 1 and 2, so its IDF is ln1.5≈0.4055. Resume 2 scores 41⋅0.4055≈0.1014 and resume 1 scores 51⋅0.4055≈0.0811. "the" is a stopword, so the third search looks for "sql" alone, which only resume 3 contains: 51ln3≈0.2197.
The second sequence shows a repeated word inside a resume and a tie:
"go" is in two of the three resumes, so its IDF is ln1.5. Resume 10 has it in 2 of its 3 tokens (32⋅0.4055≈0.2703) and resume 20 in 1 of its 2 tokens (≈0.2027). For "kubernetes go" both resumes score ln1.5≈0.4055, so the tie goes to the lower ID, 10, even though resume 20 was added first. No resume contains both "go" and "rust", so the last search is empty.
Constraints & Assumptions
1 <= docId <= 10^5
Document content is a non-empty string of alphanumeric characters and spaces.
The query is a non-empty string.
At most
10^4
calls to
addDocument
and
search
combined.
Scores are rounded to 4 decimal places.
Clarifying Questions to Ask Guidance
What should
addDocument
do with a docId that is already stored: replace the old resume, ignore the call, or raise an error?
Does
N
count a resume whose text is made entirely of stopwords?
Will resumes ever be edited or removed, so that the index has to support deletes?
Should
search
return every match, or only the top few results?
What a Strong Answer Covers Guidance
A single tokenizer that applies lowercasing, splitting and stopword removal the same way to resumes and queries.
The data structures for the document store, per-resume term counts and lengths, and the inverted index, and how
addDocument
keeps them consistent.
How
search
restricts the candidates to resumes containing every query term, and what that step costs.
The exact scoring arithmetic: repeated query words, terms that appear in every resume, rounding, and the tie rule.
Edge cases such as a query that tokenizes to nothing, unknown terms, a re-added docId, and resumes with no indexable tokens.
Follow-up Questions Guidance
How would you support removing a resume, and what happens to
N
,
df
and the scores of other resumes when you do?
The collection grows to millions of resumes and recruiters only look at the top 10 results. What would you change in the intersection and ranking steps?
How would you switch the ranking to BM25, and what extra state would the index need?
How would you make the index safe when
addDocument
and
search
run concurrently?