Access-Tracked Key-Value Store
Data Structure DesignAuto-graded · 16 test casesLast updated
Build a dictionary that counts how many times each key has been retrieved, testing hash map design and state management.
Implement a key-value store that tracks retrieval frequency. You will receive a sequence of operations to perform and must return the result of each operation in order.
Your store must support four operations:
put: Insert or update a key-value pair. When a key is first inserted, initialize its access count to 0. Updating an existing key's value does not reset its count.get: Retrieve the value for a key. If the key exists, increment its access count by 1 and return the value. If the key does not exist, returnNone.delete: Remove a key and its associated access count. Deleting a non-existent key has no effect.get_read_count: Return the total number of successfulgetoperations for a key. Return 0 if the key does not exist or has been deleted.
Write a function that takes two parallel lists: one containing operation names and one containing their arguments. Return a list of results, one per operation. Operations that modify state return None.
Constraints
- 1 ≤ number of operations ≤ 10⁵
- Keys and values can be any hashable type or any type, respectively
- Keys are case-sensitive strings in test cases
- Access counts never overflow standard integer limits
- Each operation list corresponds exactly to one argument list entry
Examples
Example 1
Input: access_tracked_store(['put', 'get', 'get', 'get_read_count', 'delete', 'get'], [['a', 10], ['a'], ['a'], ['a'], ['a'], ['a']])
Output: [None, 10, 10, 2, None, None]
Hints
Hint 1
Use two separate dictionaries: one for values and one for access counts. Keep them synchronized on insert and delete.
Hint 2
When updating an existing key with put, preserve its access count—only newly created keys start at 0.
Hint 3
The get operation is a read-modify-write: check existence, increment the counter, then return the value.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Maintain two hash maps that mirror each other's key sets: one storing values and one storing access counts. Process each operation by updating the appropriate map(s) and collecting results.
Core insight: The access count represents the lifetime retrieval history of a key. When you overwrite a key's value with put, the key identity persists, so the count stays intact. Only when a key is created for the first time does its count initialize to 0. Deletion removes both the value and the count, so re-inserting the same key later starts fresh.
Operation details:
- put(key, value): Check if the key is new. If so, set its count to 0. Then store or overwrite the value. Append
Noneto results. - get(key): Look up the key in the store. If present, increment its count and append the value. Otherwise append
None. This is a read-modify-write, not a pure read. - delete(key): Remove the key from both maps using
pop(key, None)to avoid errors when the key is absent. AppendNone. - get_read_count(key): Return
access_counts.get(key, 0)so deleted or never-inserted keys report 0. Append the count.
Why it works: The two dictionaries always contain exactly the same keys because every insertion creates both entries and every deletion removes both. The invariant guarantees that incrementing a count never raises a KeyError, and that no phantom counters persist after deletion.
Edge cases:
- Getting a missing key returns
Noneand creates no counter. - Deleting a missing key is safe and does nothing.
- Storing
Noneas a value makesgetreturnNone, indistinguishable from a miss, but the count still increments. - Updating a key preserves its count; deleting then re-inserting resets it to 0.
Complexity
- Time: O(1) average per operation (hash map lookup and update), O(n) overall for n operations.
- Space: O(k) for k distinct keys stored, plus O(n) for the results list.
Solution
from typing import List
def access_tracked_store(operations: List[str], arguments: List[List]) -> List:
store = {}
access_counts = {}
results = []
for op, args in zip(operations, arguments):
if op == "put":
key, val = args
if key not in store:
access_counts[key] = 0
store[key] = val
results.append(None)
elif op == "get":
key = args[0]
if key in store:
access_counts[key] += 1
results.append(store[key])
else:
results.append(None)
elif op == "delete":
key = args[0]
store.pop(key, None)
access_counts.pop(key, None)
results.append(None)
elif op == "get_read_count":
key = args[0]
results.append(access_counts.get(key, 0))
return results