← Questions
Recipe storage with version history
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Glean. It tests versioning, storage and design.
Build an in-memory store for recipes that keeps every version. Each edit creates a new version; the current recipe and any past version can be read back, and nothing is ever overwritten.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
create(name: str, ingredients: list[str], steps: list[str]) -> str— stores version 1 and returns a new recipe id ("r1","r2", ...). -
update(recipe_id: str, name: str | None = None, ingredients: list[str] | None = None, steps: list[str] | None = None) -> int— creates the next version from the current one with the given fields replaced, and returns its version number. Unknown id: raiseKeyError. -
get(recipe_id: str, version: int | None = None) -> dict— the recipe at that version ({"name", "ingredients", "steps"}), the latest whenversionis omitted. -
history(recipe_id: str) -> list[int]— the version numbers, oldest first. -
revert(recipe_id: str, version: int) -> int— makes a new version whose content equals the given old one; history is kept.
Example
create("pancakes", ["flour", "milk", "egg"], ["mix", "fry"]) -> "r1"
update("r1", ingredients=["flour", "milk", "egg", "sugar"]) -> 2
update("r1", name="sweet pancakes") -> 3
get("r1") -> {"name": "sweet pancakes", "ingredients": ["flour", "milk", "egg", "sugar"], "steps": ["mix", "fry"]}
get("r1", 1) -> {"name": "pancakes", "ingredients": ["flour", "milk", "egg"], "steps": ["mix", "fry"]}
revert("r1", 1) -> 4 # version 4 has version 1's content
history("r1") -> [1, 2, 3, 4]
Constraints
- Versions are numbered from 1 per recipe and never reused or deleted.
- A returned recipe must not change if the caller mutates it, and later edits must not change stored versions.
Clarifying questions
- Do versions copy the whole recipe, or only what changed?
- Does revert rewrite history or add a new version?
- Do versions need timestamps or an author?
- Will recipes be large, or edited very often (does storage size matter)?
- Do two people edit the same recipe at once?
What a strong answer covers
- A map from recipe id to an append-only list of immutable versions; O(1) get by version.
- Defensive copies (or immutable tuples) so stored versions cannot be changed through a returned object.
- Clear errors for unknown ids and versions out of range.
- The storage trade-off: full snapshots (simple, fast reads) versus deltas from the previous version (small, slower reads), and snapshots every
kversions as the middle ground. - Tests: update one field keeps the others, revert adds a version, old versions are unchanged after later edits.
Follow-ups
-
diff(recipe_id, v1, v2): which fields and which ingredients or steps changed. - Concurrent editors:
updatetakes the version it was based on and fails if the recipe moved on. - Find recipes whose latest version uses a given ingredient.
- Persist the store so it survives a restart.
Where it's asked
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
