Design an In-Memory Search Engine
🔎 What is an in-memory search engine? It is a small library that stores text documents and finds the ones that contain the words you ask for. Documents live in named groups called datasets. You can add and delete documents at any time, and every search returns matching document ids in the order you choose: best match, newest, or by id.
Interviewers phrase this problem in several ways: "search documents inside datasets", "insert and delete documents and search by a pattern", or "return search results sorted by different criteria". They are all the same design. The datasets are separate namespaces, insert and delete keep an index up to date, a "pattern" is a word or a prefix, and the ordering is a rule you can swap. We cover each part below.
The problem looks like a string exercise, but it is really a data-structure question in disguise. The interviewer wants to see you reject the brute-force scan, reach for an inverted index, keep it correct when documents are deleted, and keep the ordering rule out of the search code.
Requirements
"Design an in-memory search engine. Users create datasets, add and delete documents, and search a dataset with a pattern. Results can be ordered in different ways."
Every word in that sentence is vague: what is a pattern, what does "different ways" mean, what happens to a deleted document? The questions below settle the ones that change the code.
Clarifying Questions
You: What does a pattern look like? A substring, a regular expression, whole words?
Interviewer: Whole words, plus prefixes. quick matches the word "quick". qu* matches any word that starts with "qu". No regular expressions.
You: If the query has several terms, must a document contain all of them, or any of them?
Interviewer: All of them. brown guide only returns documents with both words.
You: Which orderings do we need?
Interviewer: Three for now: by relevance, newest first, and by document id. Relevance is simply how many times the query terms appear in the document. We will probably add more orderings later.
You: How do we break ties, for example two documents with the same score?
Interviewer: By document id, smallest first. Results must be deterministic.
You: What is a word? Does case or punctuation matter?
Interviewer: A word is a run of letters and digits, compared in lowercase. "Quick," and "quick" are the same word.
You: Is a dataset just a folder name, or does it change behavior?
Interviewer: It is a separate space. The same document id can exist in two datasets, and a search only looks at one dataset.
You: Can a document be updated? And how do we know how new it is?
Interviewer: Adding an id that already exists fails. To change a document, delete it and add it again. Each add comes with a timestamp, which is what "newest" sorts by.
The first answer matters most. "Pattern" could mean substring search, and that leads to a completely different design (suffix arrays or n-gram indexes). Words and prefixes lead to an inverted index. Pin this down before you draw a single class.
💡 Interview tip: When a prompt says "pattern", give the interviewer two or three concrete examples and ask which ones must match:
catin "catalog"?cat*?*log? Their answers define the data structure, so this question pays for itself.
Final Requirements
- Users can create named datasets. Creating a name that already exists fails.
- A document has an id, a text and a timestamp, and belongs to one dataset. Adding a document to a missing dataset, or with an id the dataset already has, fails.
- A document can be deleted. Deleting a document that is not there fails. A deleted id can be added again.
- A word is a run of letters and digits, lowercased.
- A query is one or more terms. A term is a word (exact match) or a prefix ending in
*(matches every word that starts with it). A document matches when every term matches at least one of its words. - A document's score is the number of its words matched by each term, summed over the terms.
- Results are ordered by relevance (score, high first), newest (timestamp, high first) or id. Ties go to the smaller id.
- New orderings will be added, so adding one must not touch the search code.
Out of scope
- Substring and regular-expression search (we discuss suffix patterns in Extensibility).
- Phrase search ("exact phrase"), typo tolerance, synonyms and stemming.
- Real ranking formulas such as TF-IDF or BM25.
- Persistence, sharding and several threads at once (concurrency comes back in Extensibility).