Running Tally per Label from a Replayed Call Log
Data Structure DesignAuto-graded · 18 test casesLast updated
Replay a log of increment calls and report, for each call, how many times its key has appeared so far, testing hash map counting and a simple operation-dispatch design.
You are given a recorded log of calls made to a small counting component. The component keeps a tally for each string key. Every call to it bumps the tally of one key by one and reports the updated tally.
The log is split into two parallel lists of equal length:
operations[i]is the name of the i-th call.arguments[i]is the list of arguments passed to that call.
The only call name that appears is "incrementAndGet". Its argument list holds exactly one string, the key. When this call is made, the tally for that key goes up by one (a key never seen before starts at 0, so its first call yields 1), and the call's result is the key's tally immediately after the increase. That is, the result equals the number of calls so far, including this one, that used that key.
Replay the log from the first call to the last, in order, and return a list with the result of each call, in the same order as the log.
Calls are applied strictly one after another, and different keys never affect each other. Tallies start empty. If a call name other than "incrementAndGet" is ever encountered, raise a ValueError.
Constraints
operationsandargumentshave the same length.- Each element of
argumentsis a list whose first element is the key; keys are strings and are compared exactly (case-sensitive). - The number of distinct keys is not known in advance and may be large.
- Every tally fits in a 64-bit signed integer.
- The returned list has one integer per call, in log order.
Examples
Example 1
Input: replay_key_tallies(['incrementAndGet', 'incrementAndGet', 'incrementAndGet'], [['a'], ['b'], ['a']])
Output: [1, 1, 2]
Example 2
Input: replay_key_tallies(['incrementAndGet', 'incrementAndGet', 'incrementAndGet', 'incrementAndGet'], [['x'], ['x'], ['x'], ['x']])
Output: [1, 2, 3, 4]
Example 3
Input: replay_key_tallies(['incrementAndGet', 'incrementAndGet', 'incrementAndGet', 'incrementAndGet', 'incrementAndGet'], [['cat'], ['dog'], ['cat'], ['cat'], ['dog']])
Output: [1, 1, 2, 3, 2]
Hints
Hint 1
Keep a dictionary that maps each key to how many times you have seen it so far.
Hint 2
For an unseen key, treat its current tally as 0, add one, store it, and append the stored value to the output.
Hint 3
One pass over the log is enough; each lookup and update is expected constant time, so overall work is linear in the number of calls.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Maintain a dictionary from key to its current tally. Walk through the log in order. For each call:
- Check that the call name is
"incrementAndGet"; otherwise raiseValueError. - Read the key from the first argument.
- Look up the key's tally, defaulting to 0 when absent, add one, and store the result.
- Append the new tally to the output list.
Why it is correct
The log is replayed sequentially, so there is no interleaving to worry about. The dictionary value for a key always equals the number of earlier calls with that key. Adding one on each call makes the stored value equal to the count including the current call, which is exactly the required return value. Since each key has its own dictionary entry, keys never influence one another, and unseen keys naturally start from 0.
Complexity
- Time: O(n) expected for n calls, since each dictionary operation is expected O(1).
- Space: O(k) for k distinct keys, plus O(n) for the output list.
Reference solution
from typing import List
def replay_key_tallies(operations: List[str], arguments: List[List[str]]) -> List[int]:
tallies = {}
results = []
for operation, args in zip(operations, arguments):
if operation != "incrementAndGet":
raise ValueError("unsupported operation")
key = args[0]
tallies[key] = tallies.get(key, 0) + 1
results.append(tallies[key])
return results