KatInterview

LRU Cache Simulator with Recency and Grouped Views

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure DesignLRU cacheOrderedDicthash map

Last updated

Simulates an LRU cache through a sequence of put, get, and snapshot operations, testing recency bookkeeping, eviction, and ordered grouping of values.

You are given a fixed-capacity cache and a list of operations to run against it, in order. Each operation returns one result. Your job is to simulate the cache and return the list of results.

Let capacity be the maximum number of entries the cache may hold. The cache keeps a recency order: the entry that was used most recently is "most recent," and the entry that was used least recently is "least recent."

The operations are given as two parallel lists: operations[i] is the name of the operation, and arguments[i] is its argument list.

OperationArgumentsEffectResult
"put"[key, value]Insert the key, or overwrite its value if it already exists. Either way the key becomes the most recent entry. If the cache now holds more than capacity entries, remove the least recent entry.None
"get"[key]If the key is present, make it the most recent entry and return its value. If it is absent, change nothing.The value, or None if the key is absent
"snapshot"[]Read-only. Produce one line per entry in the form key: value, ordered from most recent to least recent. Join the lines with "\n".A string (empty string if the cache is empty)
"compressed_snapshot"[]Read-only. Group the entries by value and produce one line per group in the form value [k1, k2, ...], joined with "\n".A string (empty string if the cache is empty)

Rules for the compressed view

  • Scan the entries from most recent to least recent. A group is created when its value is first encountered, so groups are ordered by the recency of their most recent member.
  • Within a group, the keys are listed from most recent to least recent.
  • Keys are joined with ", " inside the brackets.
  • Values are compared with ==, so values that compare equal (for example 1, 1.0, and True) belong to the same group. The label printed for a group is the first value encountered for it during the scan.
  • Values are rendered with str(), the same way an f-string would render them.

Other rules

  • Snapshot operations do not change recency. Only put and a successful get do.
  • A get on a missing key returns None and changes nothing.
  • Overwriting an existing key with put counts as a use. It never causes eviction, because the number of entries does not change.
  • If capacity is 0, every put is immediately evicted, so the cache always stays empty.

Constraints

  • 0 <= capacity <= 10^5
  • len(operations) == len(arguments), and both are at most 10^5
  • Every operation is one of "put", "get", "snapshot", or "compressed_snapshot"
  • Keys are non-empty strings
  • Values can be any printable objects that support ==
  • Results must be returned in the same order as the operations

Examples

Example 1

Input: simulate_lru_snapshots(2, ['put', 'put', 'snapshot', 'compressed_snapshot'], [['A', 2], ['B', 2], [], []])

Output: [None, None, 'B: 2\nA: 2', '2 [B, A]']

Example 2

Input: simulate_lru_snapshots(2, ['put', 'put', 'get', 'put', 'snapshot', 'compressed_snapshot'], [['A', 1], ['B', 2], ['A'], ['C', 1], [], []])

Output: [None, None, 1, None, 'C: 1\nA: 1', '1 [C, A]']

Hints

Hint 1

Keep the entries in an ordered structure where the front is the least recent entry and the back is the most recent. Moving an existing key to the back and removing from the front should both be constant time.

Hint 2

Snapshots only read the structure. Iterate over the entries in reverse to get most-recent-first order, and do not move anything.

Hint 3

For the compressed view, scan the entries from most recent to least recent and append each key to the group for its value. Create a new group the first time a value appears, so group order follows first appearance.