KatInterview

Simulate LRU Cache with Composite Keys

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designlru-cachehashingordered-dict

Last updated

Tests understanding of LRU eviction policy, composite key normalization, and cache statistics tracking through command simulation.

You are building a simulator for an LRU (Least Recently Used) cache that handles complex keys. The cache must support three operations:

Operations:

  1. get: Retrieve or store a value for a given key. If the key exists in the cache, return the stored value and mark it as recently used. If the key does not exist, store the provided value and return it. This counts as a cache hit or miss respectively.

  2. clear: Remove all entries from the cache and reset hit/miss counters to zero.

  3. cache_info: Return current cache statistics as a dictionary with keys "hits", "misses", "maxsize", and "currsize".

Key Normalization:

Keys can be composite structures (lists, dicts, tuples, sets) and must be normalized so that semantically equivalent structures map to the same cache entry:

  • Dictionaries with the same key-value pairs are equivalent regardless of insertion order
  • Lists and tuples are compared by position
  • Sets are equivalent if they contain the same elements

LRU Eviction:

When inserting a new entry would exceed maxsize, evict the least recently used entry first. If maxsize is 0, never store anything (always miss).

Function Signature:

def simulate_lru_cache(maxsize: int, operations: List[str], arguments: List[List]) -> List:

Parameters:

  • maxsize: Maximum number of entries the cache can hold (0 means no caching)
  • operations: List of operation names ("get", "clear", or "cache_info")
  • arguments: Parallel list of arguments for each operation
    • For "get": [key, value] where key is the lookup key and value is the value to store on miss
    • For "clear" and "cache_info": empty list []

Returns:

A list of results, one per operation:

  • "get": the cached or newly stored value
  • "clear": None
  • "cache_info": dictionary with "hits", "misses", "maxsize", "currsize"

Constraints:

  • 1 ≤ len(operations) ≤ 1000
  • 0 ≤ maxsize ≤ 100
  • Keys may contain nested lists, dicts, tuples, and sets up to 5 levels deep
  • All values are JSON-serializable primitives or structures
  • len(operations) == len(arguments)

Examples

Example 1

Input: simulate_lru_cache(2, ['get', 'get', 'get', 'cache_info', 'get', 'cache_info', 'clear', 'cache_info'], [[[1], 10], [[2], 20], [[1], 99], [], [[3], 30], [], [], []])

Output: [10, 20, 10, {'hits': 1, 'misses': 2, 'maxsize': 2, 'currsize': 2}, 30, {'hits': 1, 'misses': 3, 'maxsize': 2, 'currsize': 2}, None, {'hits': 0, 'misses': 0, 'maxsize': 2, 'currsize': 0}]

Example 2

Input: simulate_lru_cache(1, ['get', 'get', 'get', 'cache_info'], [[[1, {'b': 2, 'a': 1}], 5], [[1, {'a': 1, 'b': 2}], 99], [[2], 7], []])

Output: [5, 5, 7, {'hits': 1, 'misses': 2, 'maxsize': 1, 'currsize': 1}]

Example 3

Input: simulate_lru_cache(0, ['get', 'get', 'cache_info'], [[['x'], 1], [['x'], 2], []])

Output: [1, 2, {'hits': 0, 'misses': 2, 'maxsize': 0, 'currsize': 0}]

Hints

Hint 1

Use a data structure that preserves insertion order and supports efficient move-to-end operations

Hint 2

Convert unhashable types like dicts and lists into a hashable representation by recursively encoding their structure

Hint 3

When normalizing dict keys, sort the key-value pairs to ensure consistent hashing regardless of insertion order