KatInterview

Building an In-Memory Recipe Store with Search and Versioning

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designin-memory-storeindexing

Last updated

Design an in-memory recipe system with CRUD, ingredient lookup, per-user attribution with sorting, and immutable version history that survives deletes.

You are asked to sketch (and, time permitting, code) an in-memory recipe store. No UI and no persistence — the grader cares about API shape, choice of data structures, and how you treat edge cases. There are four progressively harder requirements.

Tier 1 — CRUD. A recipe has a name (the unique key), an ordered list of ingredients, and an ordered list of steps. Support create, read, update, delete.

Tier 2 — Ingredient lookup. Return every currently active recipe that uses a given ingredient.

Tier 3 — Users and ordering. Introduce users. Every mutating call must name the user performing it, and each recipe tracks who touched it last. Provide a listing operation that sorts by a caller-chosen key (name, ingredient count, step count, …) in ascending or descending order.

Tier 4 — Version history. Every create and every update produces an immutable snapshot recording the content, a version number, a timestamp, and the editing user. You must be able to list versions of a recipe and search history by recipe name or by editor. History outlives deletion: even after the active recipe is removed, its versions stay queryable.

State your class/API design, your data structures (including any secondary indexes), complexity per operation, and how you decide corner cases: duplicate or reused names, ingredient normalization, partial updates, deleting then recreating a recipe, unknown users, and invalid sort keys.

Hints

Hint 1

One dictionary holds live recipes and a separate dictionary holds history — never let a delete touch the history structure, or Tier 4 breaks.

Hint 2

Maintain inverted indexes (ingredient → set of names, editor → list of versions) so queries don't scan everything; update them at exactly the three mutation points.

Hint 3

Make snapshots frozen (immutable tuples/dataclasses) so a caller holding a returned reference can't rewrite history.

Hint 4

Decide explicitly whether version numbers reset after delete-and-recreate — continuing the lineage keeps (name, version) a permanent unique key.