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
Problem Analysis
In this problem, we need to design a key-value store that not only allows for standard operations like get and put but also incorporates time-based expiration of entries. The critical pattern that...
Approach
The step-by-step algorithm to implement the key-value store with expiration is as follows:
- Data Structures: Use a HashMap to store key-value pairs alongside their expiration timestamps. For ins...