Design in-memory DB with TTL and history
Company: ZipHQ
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Technical Screen
Design a simplified in-memory database to be implemented in four progressive levels, unlocking each after passing tests for the current one.
Level 1 (CRUD): Implement basic operations over records, fields, and values. Provide APIs to create/delete a record; upsert/delete a field within a record; read a whole record; read a specific field. Define behavior for non-existent records/fields and idempotency.
Level 2 (Filtered field view): Add the ability to list fields of a given record that satisfy filter predicates (equality, inequality, numeric comparison, substring/regex as appropriate). Specify a filter expression format and implement list_fields(recordId, filter).
Level 3 (Per-record TTL): Support setting a Time-To-Live per record so that a record automatically expires after its TTL elapses. Define the time source, expiry checks/cleanup strategy, and semantics for reads/writes on expired records. Target efficient expiry (e.g., O(log n) per update or better).
Level 4 (Look-back queries): Support querying values as of a past timestamp. Define how creates, updates, deletes, and TTL interact with historical reads. Specify storage approach (e.g., versioning, append-only log, snapshots), API design, and trade-offs in time/space.
For each level, specify API signatures, core data structures and algorithms, complexity analysis, key edge cases, and a minimal test plan. Optionally discuss concurrency assumptions (single-threaded vs. multi-threaded) and persistence scope (in-memory only).
Quick Answer: This question evaluates system design skills around building an in-memory database, focusing on API design, data structures, time/version semantics (TTL and time-travel), filtered querying, and complexity reasoning.