KatInterview

Recency-Evicting Store Simulation Plus Graph Group Tally

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designlru-cachehash-mapordered-dict

Last updated

Simulate a fixed-size recency-evicting key-value store from a command log, and separately count connected groups in an undirected graph, returning both results together.

This task combines two independent sub-problems. Your function must solve both and return the results together.

Part A: Recency-evicting store

A store holds integer keys mapped to integer values and has a fixed capacity. It is driven by a log of commands, given as two parallel lists: operations (command names) and arguments (the argument list for each command). The i-th command is operations[i] with arguments arguments[i].

The supported commands are:

  • "LRUCache" with arguments [capacity]: create an empty store with the given positive capacity. Output None for this command.
  • "put" with arguments [key, value]: store value under key.
    • If key already exists, overwrite its value. No eviction happens.
    • If key is new and the store is already full, first remove the least recently used key, then insert the new one.
    • In either case, key becomes the most recently used key.
    • Output None for this command.
  • "get" with arguments [key]: if key exists, output its value and make it the most recently used key. Otherwise output -1 (recency is unchanged).

Each command contributes exactly one entry to the output list, in the order the commands appear. Every get and put should run in O(1) average time.

Part B: Count connected groups

You are given an integer n and a list edges. Nodes are labeled 0 to n - 1. Each edges[i] = [a, b] is an undirected connection between nodes a and b. Count the connected components of this graph. A node with no edges is its own component. Self-loops ([a, a]) and duplicate edges may appear and must not affect correctness.

Return value

Return a dictionary with two keys:

  • "cache": the list of outputs from Part A, one per command.
  • "components": the integer component count from Part B.

Constraints

  • operations and arguments have the same length, up to about 10^5.
  • The first command is "LRUCache", and capacity is at least 1.
  • Keys and values are integers.
  • 1 <= n <= 10^5, with about up to 10^5 edges.
  • For every edge, 0 <= a, b < n.
  • Both parts are independent: the graph does not depend on the store, and vice versa.

Examples

Example 1

Input: simulate_store_and_count_groups(['LRUCache', 'put', 'put', 'get', 'put', 'get', 'put', 'get', 'get', 'get'], [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]], 5, [[0, 1], [1, 2], [3, 4]])

Output: {'cache': [None, None, None, 1, None, -1, None, -1, 3, 4], 'components': 2}

Example 2

Input: simulate_store_and_count_groups(['LRUCache', 'put', 'put', 'put', 'get', 'get'], [[2], [1, 10], [2, 20], [1, 11], [2], [1]], 4, [[0, 1]])

Output: {'cache': [None, None, None, None, 20, 11], 'components': 3}

Hints

Hint 1

For the store, you need constant-time lookup by key and constant-time reordering by recency. An ordered hash map, or a hash map plus a doubly linked list, does both.

Hint 2

Be careful with `put` on an existing key: it should refresh recency and overwrite the value, but never trigger eviction. Evict only when a genuinely new key pushes the size beyond capacity.

Hint 3

For the graph, start with `n` components and merge with Union-Find (or run a BFS/DFS from each unvisited node). Self-loops and duplicate edges naturally cause no extra merges, and isolated nodes are already counted.