Interview conceptCoding & Algorithms

In-Memory Stateful API Design

Asked of: Software Engineer

Last updated

Clean architecture diagram: client -> API/command parser -> core in-memory service with Primary Map and Secondary Indexes, Lazy TTL expiration, Snapshot/Backup, State Machine Validator, Sorted indexes, and arrows showing atomic updates and flows.

What's being tested

These problems test stateful in-memory API design: choosing data structures, defining method semantics, and preserving correctness across updates, expiration, snapshots, and state transitions. Interviewers are probing whether you can turn ambiguous product-like operations into deterministic code with clear invariants, edge-case handling, and stated complexity.

Patterns & templates

  • Primary map + secondary indexes — store canonical objects by id, then maintain user_id, prefix, status, or priority indexes; update all indexes atomically.

  • Lazy TTL expiration — implement isExpired(now, item) and purge on read/write; cheaper than background cleanup, but scans must filter expired entries.

  • Snapshot semanticsbackup(timestamp) should serialize only live records; restore(timestamp) usually restores the latest backup at or before target time.

  • State machine validation — define allowed transitions like OPEN -> FILLED/CANCELED; make invalid or repeated transitions explicit no-ops or errors.

  • Idempotent operations — repeated cancel(order_id) or assign(task_id) should not corrupt counters, balances, quotas, or indexes.

  • Sorted retrieval — use heapq, bisect, TreeMap-style structures, or sorted lists for priority/top-k/prefix scans; state complexity clearly.

  • Command/API parsing layer — separate parsing from core methods like create_account, transfer, set_with_ttl, list_tasks; this keeps tests targeted and readable.

Common pitfalls

Pitfall: Updating the primary dictionary but forgetting to update secondary indexes, causing stale results in list_by_user, prefix scans, or status queries.

Pitfall: Treating TTL as deletion only at write time; reads, backups, restores, and scans must all respect expiration at the provided timestamp.

Pitfall: Hand-waving complexity with “it’s in memory”; interviewers expect O(1) lookup, O(log n) indexed updates, or O(n) scans to be stated precisely.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Featured in interview prep guides

Practice questions

Related concepts

In-Memory Stateful API Design — Tech Interview Concept | PracHub