Design a Key-Value Store with Expiration
Last updated: February 21, 2025
Quick Overview
Implement a key-value store that supports get, put, and automatic expiration of entries after a specified time-to-live (TTL).
Rivian
February 21, 202514
5
1,825 solved
Implement a key-value store that supports get, put, and automatic expiration of entries after a specified time-to-live (TTL).
TTL-based caching is used extensively in Rivian's vehicle software for session tokens, cached API responses, and temporary configuration overrides. This tests your ability to design a practical data structure that handles time-based operations efficiently.
What the Interviewer Expects
- Implement get and put with TTL support using a hash map and expiration tracking
- Handle lazy expiration on access and periodic cleanup
- Achieve O(1) average time for get and put operations
- Handle edge cases like overwriting existing keys with new TTLs
- Discuss the tradeoffs between lazy and eager expiration strategies
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 implement periodic cleanup without blocking read/write operations?
- What if you needed to support sliding TTL that resets on each access?
- How would you make this distributed across multiple nodes?
- How does this compare to Redis's expiration implementation?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Data Structure Design
Use a hash map where each value is stored alongside its expiration timestamp (current_time + TTL). A secondary data structure (sorted set or min-heap)...
Expiration Strategies
Two strategies work together. Lazy expiration: on every get, check if the entry is expired. If so, delete it and return not found. This is O(1) but me...