Implement a Recoverable In-Memory Key-Value Store
Company: OpenAI
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
# Implement a Recoverable In-Memory Key-Value Store
Design and implement a single-process key-value store whose working set is held in memory and whose state can be recovered from a local append-only log after a process crash.
Support `put(key, value)`, `get(key)`, and `delete(key)`. Keys and values are byte strings. State the durability guarantee of a successful write, define the on-disk record format, and explain startup recovery. Include corruption handling, concurrent callers, log growth, and safe compaction.
### Constraints & Assumptions
- There is one process and one active writer for the log.
- The process can crash during any record write.
- Recovery must never invent a write that was not completely recorded.
- Values may contain arbitrary bytes, so delimiters alone are insufficient.
### Clarifying Questions to Ask
- Must each successful mutation survive power loss, or only process restart?
- Is compare-and-set or multi-key atomicity required?
- How large may the data set and log become?
- May startup discard a torn final record, and what should happen on corruption in the middle?
### What a Strong Answer Covers
- An in-memory map plus serialized append path
- Length-delimited, versioned records with operation type and checksum
- A precise write, flush, and acknowledgment order
- Deterministic replay and safe handling of a partial tail
- Locking or single-writer coordination for concurrent calls
- Snapshot or compaction with atomic file replacement
- Failure injection and recovery tests
### Follow-up Questions
- How would you add transactions?
- How would you reduce recovery time for a very large log?
- What changes when several processes need access?
- How would you detect storage corruption that is not confined to the tail?
Quick Answer: Build a single-process in-memory key-value store that can recover from its local append-only history after a crash. Define its durability contract and address torn writes, corruption, concurrent callers, startup cost, compaction, arbitrary byte values, and failure testing.