← Questions

In-memory note-taking system with keyword search

0
AnthropicCoding

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_note returns a new id: "note1", "note2", and so on. Ids are never reused, even after a delete. Both created_at and modified_at are set to timestamp.
  • get_note returns the note (id, title, content, created_at, modified_at), or None if it does not exist. Changing the returned object must not change the stored note.
  • update_note changes only the fields that are given and sets modified_at to timestamp. It returns False for an unknown id. A call with no fields changes nothing. Raise ValueError if timestamp is earlier than the note's modified_at.
  • delete_note returns True if the note existed, else False.
  • list_notes returns all ids, most recently modified first. Ties go to the note created first.
  • search splits 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 as list_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 search match 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_notes ties 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_note returning 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.
  • search intersecting the posting sets starting from the smallest, then ordering the hits like list_notes.
  • list_notes order 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding