Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 per GET, in order. capacity is 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 ttl is 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.
CommandWhat it doesPrints
PUT key value ttlStore value under keynothing
GET keyRead keythe value, or MISS
ADVANCE deltaMove the clock forward (Part 2)nothing

Part 1: Least-recently-used eviction

  1. PUT stores value under key, replacing any earlier value, and makes key the most recently used. Its ttl has no effect until Part 2.
  2. GET key prints the stored value and makes key the most recently used.
  3. If key is not in the cache, GET prints MISS. A miss changes nothing.
  4. When a PUT adds a new key to a cache that already holds capacity keys, the least recently used key is evicted to make room.
  5. 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.

  1. ADVANCE delta moves the clock forward by delta, which is zero or more.
  2. A PUT at time now gives its entry the expiry time expires_at = now + ttl.
  3. The entry is expired once now >= expires_at. An entry written at time 0 with ttl 3 can be read at time 2 but not at time 3.
  4. An expired entry is never returned. GET prints MISS for it, and that miss does not change recency.
  5. Expired entries are removed before the capacity check, so an expired entry can never cause a live key to be evicted.
  6. 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.

Example
Input
2
7
PUT a 10 5
PUT b 20 10
GET a
PUT c 30 10
GET b
GET a
GET c
Output
10
MISS
10
30
Explanation

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.