Key-Value Store with TTL
An in-memory key-value store where every write carries a time-to-live — the Redis EXPIRE model, with logical timestamps so behavior is fully deterministic.
Requirements
- Time is logical: every operation carries its own integer timestamp. Never read a real clock.
- An entry set at time
swith ttldis visible for reads at timess ≤ t < s + d(expiry instant itself is expired). - Re-setting a key replaces both value and TTL — the old TTL is gone.
- The judge only queries times at or after an entry's set time, so the expiry bound is the only boundary you must get right.
- Expired entries may be kept or lazily removed — behavior, not memory, is judged.
API to implement
Implement the class KVStore. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| set <key> <value> <timestamp> <ttl> | — | Store value under key at timestamp. The entry is readable for timestamps t with timestamp ≤ t < timestamp + ttl. A newer set fully replaces the old entry and its TTL. |
| get <key> <timestamp> | string | Return the live value for key at timestamp, or the string null if the key is absent or expired. |
Input format
The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
5
set user1 alice 1 5
get user1 3
get user1 5
get user1 6
get ghost 2alice
alice
null
null5
set k v1 0 10
set k v2 4 2
get k 5
get k 6
get k 8v2
null
null