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
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:

  1. Data Structures: Use a HashMap to store key-value pairs alongside their expiration timestamps. For ins...

Submit Your Answer
Markdown supported

Related Questions