KatInterview

Duplicate Linked List and Simulate LRU Cache Operations

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designlinked-listshash-mapdesign

Last updated

Tests linked list deep copying with arbitrary pointers and implementing an LRU cache with O(1) operations using hash maps and doubly-linked lists.

You are given a linked list representation and a sequence of cache operations to perform.

Part 1: Linked List Duplication

The linked list is represented by two parallel arrays:

  • values: the node values in order
  • random_indices: for each node, the index of the node its random pointer targets, or null if it points nowhere

Create a deep copy of this list structure. Your copy must preserve all node values and maintain identical random pointer relationships, but use entirely separate node objects.

Part 2: LRU Cache Simulation

Execute a sequence of cache operations. Each operation is one of:

  • "init" with argument [capacity]: initialize an LRU cache with the given capacity
  • "get" with argument [key]: retrieve the value associated with the key, or return -1 if not present. Mark this key as recently used.
  • "put" with argument [key, value]: insert or update the key-value pair. Mark this key as recently used. If the cache exceeds capacity after insertion, evict the least recently used key.

Both get and put operations must run in O(1) average time.

Input Format

  • values: list of integers representing node values in order
  • random_indices: list where each element is either an integer index or null
  • operations: list of operation names
  • arguments: list of argument lists, one per operation

Output Format

Return a list containing exactly three elements:

  1. The copied values array (same as input)
  2. The copied random_indices array (same as input)
  3. A list of results from each operation: None for "init" and "put", the retrieved/returned value for "get"

Constraints

  • 0 <= len(values) <= 1000
  • 0 <= values[i] <= 10000
  • random_indices[i] is either null or a valid index into values
  • 1 <= len(operations) <= 1000
  • The first operation is always "init"
  • 1 <= capacity <= 3000
  • 0 <= key, value <= 10000

Examples

Example 1

Input: duplicate_list_and_execute_cache([7, 13, 11, 10, 1], [None, 0, 4, 2, 0], ['init', 'put', 'put', 'get', 'put', 'get', 'get', 'put', 'get', 'get', 'get'], [[2], [1, 1], [2, 2], [1], [3, 3], [2], [1], [4, 4], [1], [3], [4]])

Output: [[7, 13, 11, 10, 1], [None, 0, 4, 2, 0], [None, None, None, 1, None, -1, 1, None, 1, -1, 4]]

Example 2

Input: duplicate_list_and_execute_cache([], [], ['init', 'get', 'put', 'get'], [[1], [5], [5, 9], [5]])

Output: [[], [], [None, -1, None, 9]]

Example 3

Input: duplicate_list_and_execute_cache([1, 2], [1, 0], ['init', 'put', 'put', 'get', 'put', 'get', 'get'], [[1], [1, 10], [2, 20], [1], [3, 30], [2], [3]])

Output: [[1, 2], [1, 0], [None, None, None, -1, None, -1, 30]]

Hints

Hint 1

For the linked list copy: use a hash map to track the correspondence between original and cloned nodes, then wire pointers in a second pass.

Hint 2

For the LRU cache: combine a hash map for O(1) lookups with a doubly-linked list to maintain access order.

Hint 3

Use sentinel head and tail nodes in your doubly-linked list to avoid special cases when inserting or removing at boundaries.