Q217FreeFirmware
Expiring Key-Value Store
Interview prompt
Question
Design a cycle-based key-value store in which every entry has a time-to-live (TTL). Expired values must never be returned, and expiration processing must avoid scanning the entire map every cycle.
Starting point
Question code
class ExpiringStore #(type key_t = int, type value_t = int);
void put(key_t key, value_t value, int unsigned ttl);
bit get(key_t key, output value_t value);
bit erase(key_t key);
void tick();
endclassReviewed example
Trace one case
Input
t=10: put(key=7, value=0xAA, ttl=5)
t=14: get(7)
t=15: get(7)Expected output
t=14 -> 0xAA
t=15 -> MISSThe value is readable before its absolute expiry time and is removed or ignored exactly at expiry.
What to cover
Requirements
- Define precisely whether ttl=1 survives the current cycle or the next cycle.
- Updating an existing key replaces both its value and expiration time.
- Ignore stale expiration records created by prior updates to the same key.
- Make expiration work proportional to entries that actually expire plus scheduling overhead, not a full-map scan.

