KatInterview

Execute In-Memory Key-Value Storage Commands

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashingarrayshash-mapdata-structures

Last updated

Simulate an in-memory key-value dictionary given a sequence of insertion, lookup, and deletion instructions.

You are tasking with implementing the execution engine for an in-memory key-value database. The system processes a sequence of instructions sequentially.

Each instruction in the list operations is formatted as one of the following command lists:

  • ["SET", "key", "value"]: Stores value corresponding to key. If key already exists, update its value.
  • ["GET", "key"]: Retrieves the current value mapped to key. If key does not currently exist in the store, output "null".
  • ["DELETE", "key"]: Removes key and its assigned value. If key is not present, no change occurs.

Write a function process_operations(operations) that executes all input actions in order and returns a list containing the output of every GET instruction encountered.

Constraints

  • $1 \le \text{len(operations)} \le 10^5$
  • Each operation is a list of strings: 2 items for GET and DELETE, 3 items for SET.
  • All keys and values are non-empty strings containing only alphanumeric characters.

Examples

Example 1

Input: process_operations([['SET', 'a', '1'], ['GET', 'a'], ['DELETE', 'a'], ['GET', 'a']])

Output: ['1', 'null']

Example 2

Input: process_operations([['SET', 'x', '10'], ['SET', 'x', '20'], ['GET', 'x']])

Output: ['20']

Hints

Hint 1

Use a standard hash table / dictionary to track active key-value pairs.

Hint 2

Ensure deleting a non-existent key does not raise an exception.

Hint 3

Only record outputs into the result list when processing a 'GET' command.