Conversation Session State With Nested Transactions and Rollback
Last updated: November 29, 2026
Quick Overview
Implement a key-value store for agent session state with set, get, delete, count(value), and nested begin, commit and rollback. Rollback must undo only the innermost open transaction and should cost time proportional to the keys it changed.
Sierra
November 29, 20260
0
0 solved
Implement a key-value store for agent session state with set, get, delete, count(value), and nested begin, commit and rollback. Rollback must undo only the innermost open transaction and should cost time proportional to the keys it changed.
A Sierra agent may tentatively update session state while trying a tool action, then need to undo it if the action fails or a guardrail blocks the reply. The interviewer checks an undo-log design, correct merging on nested commit, and keeping a secondary index consistent.
What the Interviewer Expects
- Use an undo log per transaction storing original values
- Record a key's prior value only the first time it is touched in a transaction
- Merge child undo entries into the parent on commit without overwriting older originals
- Keep the value count index consistent through set, delete and rollback
- Error on commit or rollback with no open transaction
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make this safe for concurrent writers in one session?
- What changes if commit must be durable across process restarts?
- How would you expose a diff of what an agent turn changed, for audit logs?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
get returns None when absent. count(v) returns how many keys currently hold value v in O(1). Transactions nest. rollback undoes the innermost transaction; commit closes the innermost one, fo...
High-Level Design
Copying the whole map on begin is simple but O(n) per transaction. Instead each open transaction keeps a dict of key to the value it had before this transaction first touched it (or a sentinel for a...