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
Coding & Algorithms
Software Engineer
Rivian
February 21, 2025
Software Engineer
Onsite - Coding
Coding & Algorithms
Medium

14

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
Hash map design
TTL and expiration handling
Lazy vs eager cleanup strategies
Time-based data structures
Concurrency considerations
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions