MediumStateful DesignPython 3
TTL Key/Value Store
Build a deterministic in-memory store where every read path treats expired keys as missing.
35m3 sample tests5 hidden tests
Implement an in-memory Store with set, get, delete, and scan.
Requirements
Store(now)receives a zero-argument clock function.set(key, value, ttl=None)stores a value.ttlis measured in seconds.get(key)returns the value orNonewhen missing or expired.delete(key)removes a key and returnsTrueif it existed and was not expired.scan(prefix)returns sorted(key, value)pairs for non-expired keys whose key starts withprefix.- Expiry is inclusive: a key is expired when
now() >= expires_at.
Example
python
1clock = {"t": 0}
2store = Store(lambda: clock["t"])
3store.set("user:1", "Ada", ttl=10)
4assert store.get("user:1") == "Ada"
5clock["t"] = 10
6assert store.get("user:1") is NoneConstraints
- Don't use
sleepin tests or implementation. - Use standard-library Python only.
Editor