Key-Value Store Operation Simulator
Data Structure DesignAuto-graded · 13 test casesLast updated
Tests ability to use hash maps for constant-time operations and handle state transitions in a key-value store simulation.
You are building a simulator for a key-value storage system. Your task is to process a sequence of operations on an initially empty store and return the results.
You will receive two parallel lists:
operations: a list of operation namesarguments: a list of argument lists, wherearguments[i]contains the parameters foroperations[i]
Your function must process each operation in order and collect results into a list.
Operations
"insert" — Arguments: [key, value]
- Store the key-value pair
- If the key already exists, replace its value with the new one
- Append
Noneto the results
"get" — Arguments: [key]
- Retrieve the value associated with the key
- Append the value to the results, or
Noneif the key is not present
"delete" — Arguments: [key]
- Remove the key and its value from the store if it exists
- If the key is not present, do nothing
- Append
Noneto the results
Keys and values are both strings. An empty string is a valid key or value.
Constraints
- 1 ≤ len(operations) ≤ 10⁵
- len(operations) == len(arguments)
- All keys and values are strings with length ≤ 100
- Each operation name is one of
"insert","get", or"delete"
Examples
Example 1
Input: simulate_key_value_store(['insert', 'get', 'insert', 'get', 'delete', 'get'], [['name', 'Ada'], ['name'], ['name', 'Grace'], ['name'], ['name'], ['name']])
Output: [None, 'Ada', None, 'Grace', None, None]
Hints
Hint 1
A hash map provides average O(1) time for lookups, insertions, and deletions.
Hint 2
Use dictionary methods that handle missing keys gracefully rather than checking existence explicitly.
Hint 3
The result list must have exactly one entry per operation, with None representing both 'no return value' and 'key not found'.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that a hash map (Python's dict) naturally supports all three operations with average constant time complexity.
We maintain a single dictionary representing the current state of the store. For each operation in sequence:
- Insert: Assign the value to the key using
store[key] = value. Dictionary assignment automatically handles both new keys and updates to existing keys. - Get: Use
store.get(key)which returns the associated value orNoneif the key is absent, without raising an exception. - Delete: Use
store.pop(key, None)which removes the key if present and does nothing if it's missing, avoidingKeyError.
Each operation appends exactly one result, maintaining alignment between the operations list and results list.
Why It Works
The dictionary maintains the invariant that it contains exactly those keys that have been inserted and not subsequently deleted, each mapping to its most recent value. Operations are processed sequentially, so state transitions follow insertion order. The None value serves dual purpose: indicating operations with no return value (insert, delete) and signaling key-not-found for get operations.
Critical implementation details:
- Using
dict.get()prevents exceptions on missing keys - Using
dict.pop(key, None)safely removes keys whether they exist or not - Direct assignment
store[key] = valuehandles both insert and update without conditional logic
Complexity
Time: O(m) where m is the number of operations. Each hash map operation (assignment, get, pop) takes average O(1) time, amortized over resizing. Hashing a key of length L takes O(L), but since key length is bounded, we treat it as constant.
Space: O(k + m) where k is the maximum number of distinct keys stored at any point (the working set) and m is the length of the results list.
from typing import List, Optional
def simulate_key_value_store(operations: List[str], arguments: List[List[str]]) -> List[Optional[str]]:
store = {}
results = []
for operation, args in zip(operations, arguments):
if operation == "insert":
key, value = args
store[key] = value
results.append(None)
elif operation == "get":
key = args[0]
results.append(store.get(key))
elif operation == "delete":
key = args[0]
store.pop(key, None)
results.append(None)
return results