KatInterview

Call-Signature Cache with Least-Recently-Used Eviction

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designlru-cachehash-mapdoubly-linked-list

Last updated

Simulate a fixed-capacity LRU cache keyed by a call's positional and keyword arguments, with order-insensitive, type-aware key matching.

Call-Signature Cache

You are simulating a cache that remembers the results of function calls. A call is identified by its positional arguments (a list) and its keyword arguments (a dictionary). The cache holds at most capacity entries. When it overflows, it discards the least recently used entry.

Implement a function that processes a sequence of operations on an initially empty cache and returns one result per operation.

Operations

Operation i is operations[i], with its data in arguments[i]:

Operationarguments[i]EffectResult
"put"[positional, keyword, value]Store value under the call identified by positional and keyword.None
"get"[positional, keyword]Look up the call.The stored value, or None if the call is not in the cache
"size"[]Report how many entries are currently stored.That count
"clear"[]Remove every entry.None

Rules

  • Call identity. Two calls are the same entry exactly when their positional lists are equal and their keyword dictionaries are equal, under these rules:
    • The order of keys in a dictionary does not matter. This applies to the top-level keyword dictionary and to dictionaries nested at any depth.
    • The order of items in a list does matter, at any depth.
    • Values are compared with their types. 1, 1.0 and true are three different values. [1] and 1 are different. null is different from every other value.
    • Positional and keyword arguments are separate. Moving a value from one to the other produces a different call.
  • Recency. A put (whether it inserts a new entry or overwrites an existing one) and a successful get both make that entry the most recently used. A get that misses changes nothing. size and clear do not affect recency of any other entries.
  • Overwrite. A put for an existing call replaces its value and does not change the size.
  • Eviction. If a put of a new call makes the number of entries exceed capacity, remove the single least recently used entry. The entry just inserted counts as the most recently used.
  • A stored value may itself be None. In that case a hit and a miss both return None, but a hit still refreshes recency.

Input

  • capacity: maximum number of entries.
  • operations: list of operation names, each one of "put", "get", "size", "clear".
  • arguments: list of the same length as operations. Each element has the shape given in the table. Positional lists and keyword dictionaries contain only JSON-style values: null, booleans, integers, floats, strings, lists, and dictionaries with string keys.

Output

A list with one result per operation, in order.

Constraints

  • 1 <= capacity <= 1000
  • 0 <= len(operations) <= 10^4
  • len(arguments) == len(operations)
  • Argument structures are finite and contain only the JSON-style types listed above.
  • Stored values can be any JSON-style value.
  • Each get/put should run in O(1) time apart from the cost of examining the arguments of that call.

Examples

Example 1

Input: run_call_cache(2, ['put', 'put', 'get', 'put', 'get', 'get'], [[[1], {}, 10], [[2], {}, 20], [[1], {}], [[3], {}, 30], [[2], {}], [[3], {}]])

Output: [None, None, 10, None, None, 30]

Example 2

Input: run_call_cache(2, ['put', 'put', 'get', 'get', 'size'], [[[1, 2], {'x': 1, 'y': [2, 3]}, 5], [[1, 2], {'y': [2, 3], 'x': 1}, 6], [[1, 2], {'x': 1, 'y': [2, 3]}], [[1, 2], {'x': 1, 'y': [2, 4]}], []])

Output: [None, None, 6, None, 1]

Example 3

Input: run_call_cache(1, ['put', 'put', 'get', 'get', 'put', 'get'], [[[], {'a': [1, {'b': 2}]}, 7], [[], {'a': [1, {'b': 2}]}, 8], [[], {'a': [1, {'b': 2}]}], [[], {'a': [1, {'b': 3}]}], [['z'], {}, 9], [[], {'a': [1, {'b': 2}]}]])

Output: [None, None, 8, None, None, None]

Hints

Hint 1

Build a single hashable key from (positional, keyword). Convert each value recursively into a tagged tuple so that lists, dicts, ints, floats and booleans cannot collide with each other.

Hint 2

For dictionaries, produce a tuple of (key, converted value) pairs sorted by key so that insertion order is irrelevant. Do not sort lists.

Hint 3

Use a hash map from key to a node in a doubly linked list with sentinel head and tail nodes. Moving a node to the most-recent end and evicting the node next to the other sentinel are both O(1).