Build an in-memory key–value store in stages under a time limit: basic get/set/delete, then expiring entries, then nested transactions with rollback, and finally saving and restoring state.
Answer
Model answer, written in a candidate's voice
In short
- Define one explicit model: key -> value + optional expiry + transaction metadata.
- Build basic get/set/delete first with tests for missing, overwrite, and delete-absent cases.
- Add lazy expiry on read; test expired, not-yet-expired, and no-expiry entries.
- Add nested transactions with undo logs or snapshots; test inner rollback, outer commit, and outer rollback.
- Serialise committed state including expiry; restore through the same validation path; test round-trip and bad input.
- Refactor by layering, not rewriting; narrate trade-offs and protect edge cases under time pressure.
In full
I would start by defining the model explicitly: each key maps to a value, an optional expiry timestamp, and enough metadata to know whether it was written inside a transaction, so every later stage extends the same structure instead of replacing it. I would build the basic map first with focused tests for set/get/delete, missing keys, overwrites, and delete-of-absent-key, then add expiry lazily on read with tests for expired, not-yet-expired, and no-expiry entries, accepting that lazy expiry is simpler and cheaper than a background sweeper but can leave stale entries until touched. For nested transactions I would keep a stack of undo logs or snapshots so an inner rollback restores only the inner changes while an outer commit makes the whole tree visible, with tests for inner rollback followed by outer commit, outer rollback after inner commit, and failure mid-transaction. Persistence would serialise the committed state, including expiry timestamps, and restore it through the same validation path, with round-trip and corrupt-input tests rather than trusting the file. Under time pressure I would keep the core small, add one stage at a time, and narrate trade-offs: lazy expiry versus active cleanup, snapshots versus a write-ahead log, and memory simplicity versus durability, while not letting speed turn into untested edge cases.
What a strong answer covers
- A clean data model.
- Tests at every level.
- Edge cases: expired keys, nested rollback.
- Refactor to extend rather than rewrite.
- Keeps pace without losing correctness, and narrates the trade-offs.
Common mistakes
- Over-engineering level 1.
- Skipping tests, so bugs cascade through later levels.