KatInterview

Build an In-Memory Notebook System with Rollback

Data Structure DesignModel answer
MediumModel answerData Structure Designversioningooprollback

Last updated

Design and implement a versioned in-memory notebook system supporting CRUD, case-insensitive search, user management, and full state rollback via a change log.

In-Memory Notebook System with Version History

You are building a lightweight in-memory Notebook Management System. No database or file I/O is required — everything lives in memory. Implement it as a class with the methods described below, adding features level by level.

Data model

A Notebook has:

  • notebook_id — unique string identifier
  • title — string
  • page_count — integer (used for sorting)

A Member has:

  • member_id — unique string identifier
  • member_name — string

Level 1 — Core CRUD

Implement the four fundamental operations:

  • add_notebook(notebook) — add a new notebook. Clearly state what happens if notebook_id is already taken.
  • update_notebook(notebook_id, **fields) — update one or more fields of an existing notebook.
  • get_notebook(notebook_id) — retrieve a notebook by id, or signal that it does not exist.
  • delete_notebook(notebook_id) — remove a notebook from the system.

Describe your behavior for edge cases: updating or deleting a notebook that does not exist, updating with an unrecognized field name.


Level 2 — Search and listing

  • search_notebooks(query) — find notebooks whose title contains query (case-insensitive substring match). An empty query should match everything.
  • list_notebooks(sort_by) — return all notebooks sorted by:
    • "title" — lexicographic, case-insensitive, ties broken by notebook_id
    • "page_count" — numeric ascending, ties broken by notebook_id
    • Any other value is an error.

Level 3 — Members

  • add_member(member) — add a member to the system. State your behavior on duplicate member_id.

Level 4 — Versioning and rollback

Every successful mutating operation (add, update, delete notebook; add member) creates a new version. Implement:

  • version_history() — return a list of version metadata, at minimum a monotonically increasing version_id and a human-readable description of what changed.
  • rollback(version_id) — restore the system to exactly the state it was in immediately after version_id was committed. The rollback itself is recorded as a new version so the action can be undone.

A failed operation (e.g., adding a duplicate, deleting a non-existent notebook) must not create a version.

Aim for better than O(n) time and space per mutation for the versioning layer. Describe your design choices and their trade-offs.


Constraints

  • Up to ~100 000 notebooks and members.
  • All operations should have reasonable time complexity; state your analysis.
  • Implement in the language of your choice.

Hints

Hint 1

For levels 1–3, a plain dictionary keyed by id gives O(1) reads and writes. The interesting design work is in level 4: think about what the minimum information is that lets you reconstruct any past state without copying the entire store on every mutation.

Hint 2

A change log beats full snapshots. Instead of cloning the whole dictionary on each write, record what each mutation changed: (table, key, before, after). To roll back to version v, find every key that was touched after v and restore its value from the first change to that key after v — that 'before' value is exactly what existed at v.

Hint 3

Immutable records are your best friend here. If Notebook and Member objects cannot be mutated after creation, you can store plain references in your before/after fields without defensive copying. Use frozen dataclasses in Python, data classes in Kotlin, or records in Java. Also think carefully about which operations should be no-ops versus errors, and document those contracts up front.