In-memory note-taking system with keyword search
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Anthropic. It tests object-oriented design, hash map and inverted index.
Design an in-memory note-taking system, SecondBrainSystem. Each note gets a unique id and carries metadata about when it was created and last modified. Callers pass the current time into every write, so behavior is deterministic.
API
class SecondBrainSystem:
def __init__(self) -> None: ...
def create_note(self, timestamp: int, title: str, content: str) -> str: ...
def get_note(self, note_id: str) -> Note | None: ...
def update_note(self, timestamp: int, note_id: str,
title: str | None = None, content: str | None = None) -> bool: ...
def delete_note(self, note_id: str) -> bool: ...
def list_notes(self) -> list[str]: ...
def search(self, query: str) -> list[str]: ...
-
create_notereturns a new id:"note1","note2", and so on. Ids are never reused, even after a delete. Bothcreated_atandmodified_atare set totimestamp. -
get_notereturns the note (id, title, content,created_at,modified_at), orNoneif it does not exist. Changing the returned object must not change the stored note. -
update_notechanges only the fields that are given and setsmodified_attotimestamp. It returnsFalsefor an unknown id. A call with no fields changes nothing. RaiseValueErroriftimestampis earlier than the note'smodified_at. -
delete_notereturnsTrueif the note existed, elseFalse. -
list_notesreturns all ids, most recently modified first. Ties go to the note created first. -
searchsplits the query into lowercase words of letters and digits. It returns the ids of notes whose title or content contains every query word, in the same order aslist_notes. An empty query returns[].
Example
create_note(1, "Raft notes", "Leader election uses randomized timeouts.") -> "note1"
create_note(2, "Groceries", "eggs, milk, leader brand coffee") -> "note2"
search("leader") -> ["note2", "note1"]
search("LEADER election") -> ["note1"]
update_note(5, "note1", content="Log replication.") -> True
search("election") -> []
get_note("note1") -> title "Raft notes", created_at 1, modified_at 5
delete_note("note2") -> True
create_note(8, "c", "") -> "note3"
Constraints
- Up to 10^5 notes and 10^5 operations. Notes are up to 10^4 characters.
- Searches are frequent, so a full scan per query is too slow. State the time and space cost of each operation.
- Expect follow-up parts (for example tags, version history, or links between notes). Keep the design easy to extend.
Clarifying questions
- Does
searchmatch whole words only, or prefixes and substrings too ("lead" for "leader")? - Does a word count if it appears in the title only, the content only, or split across both?
- Does an update with the same text as before still change
modified_at? - Is the order for
list_notesties the creation time or the id number? - How long can a query be, and are duplicates in it ignored?
What a strong answer covers
- Notes by id,
get_notereturning a copy, and one tokenizer (lower-case runs of letters and digits) shared by indexing and search. - An inverted index from word to the set of note ids, updated on create, update (remove old words, add new ones) and delete.
-
searchintersecting the posting sets starting from the smallest, then ordering the hits likelist_notes. -
list_notesorder kept by sorting on(-modified_at, created_at, id)or in a sorted structure updated on writes, with the cost of each stated. - Tests: the example in full, a query with repeated or mixed-case words, punctuation in content, an update that removes a word, ids after a delete, a timestamp going backwards.
Follow-ups
- Tags, and searching by tag and words together.
- Version history per note, with restore.
- Links between notes and backlinks.
- Rank results by relevance (TF-IDF) instead of recency.
Where it's asked
What it tests
object-oriented design · hash map · inverted index
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
