TTL-Aware LRU Cache
Prerequisites: STL Container Choice, map, list, and deque Trade-Offs
A trading process caches per-symbol data from a slower service, such as the instrument record for AAPL, so a repeated lookup skips the round trip. Build that cache.
- Least recently used (LRU) key: the key whose last read or write is the oldest. A full cache evicts it when a new key arrives.
- Time to live (TTL): the number of time units an entry stays valid after it is written.
What to implement
process_commands(capacity, commands)returns one line perGET, in order.capacityis the most keys the cache may hold at once.- On stdin, the first line is the capacity and the second is the number of commands. Each command is one line.
- Keys contain no whitespace, values are signed integers, and every
ttlis positive. - Values, TTLs, and the clock all fit in a signed 64-bit integer. The clock never exceeds 10^18.
- There are two parts, and Part 2 builds on Part 1. One solution must pass all twelve tests.
| Command | What it does | Prints |
|---|---|---|
PUT key value ttl | Store value under key | nothing |
GET key | Read key | the value, or MISS |
ADVANCE delta | Move the clock forward (Part 2) | nothing |
Part 1: Least-recently-used eviction
PUTstoresvalueunderkey, replacing any earlier value, and makeskeythe most recently used. Itsttlhas no effect until Part 2.GET keyprints the stored value and makeskeythe most recently used.- If
keyis not in the cache,GETprintsMISS. A miss changes nothing. - When a
PUTadds a new key to a cache that already holdscapacitykeys, the least recently used key is evicted to make room. - Replacing a key that is already in the cache never evicts anything.
Tests 1–6 and 11 cover this part. They never move time forward, so every entry stays valid. Test 11 replaces a key while the cache is full.
Part 2: Expire stale entries
The cache now has a clock that starts at 0.
ADVANCE deltamoves the clock forward bydelta, which is zero or more.- A
PUTat timenowgives its entry the expiry timeexpires_at = now + ttl. - The entry is expired once
now >= expires_at. An entry written at time 0 withttl3 can be read at time 2 but not at time 3. - An expired entry is never returned.
GETprintsMISSfor it, and that miss does not change recency. - Expired entries are removed before the capacity check, so an expired entry can never cause a live key to be evicted.
- Updating a key refreshes both its expiry and its recency.
Tests 7–10 and 12 cover this part. In test 10 the expired key is not the least recently used one, so a cache that evicts before purging expired entries pushes out a live key.
2 7 PUT a 10 5 PUT b 20 10 GET a PUT c 30 10 GET b GET a GET c
10 MISS 10 30
The capacity is 2 keys, and 7 commands follow.
After PUT a and PUT b, a is the least recently used key. GET a reads it, which leaves b as the least recently used.
PUT c adds a third key, so b is evicted rather than a, even though a was stored first. Time never advances here, so the TTLs of 5 and 10 do not matter.