Log Event Processor with Time Order
Data Structure DesignAuto-graded · 14 test casesLast updated
Tests data structure management and stable sorting to process out-of-order time-stamped events while preserving insertion order for ties.
Design an event processing sequence executor that records timestamped messages and returns historical log states.
You need to write a function event_tracker(operations: List[str], arguments: List[List]) -> List[Optional[List[str]]] that processes a sequential list of operations applied to an empty event log.
The supported operations are:
"record": Takes[timestamp, message](wheretimestampis an integer andmessageis a string). Records the event and returnsNone."get_ordered_messages": Takes[]. Returns a list of all messages recorded so far, sorted primarily by their timestamp in ascending order. If multiple messages share the exact same timestamp, their relative order must match the sequence in which they were recorded (stable ordering).
Your function should return a list where each element corresponds to the output of the respective operation in operations.
If an unrecognised operation name is encountered, raise a ValueError.
Constraints
- $1 \le N \le 10^4$ (number of operations)
- Timestamps are non-negative integers up to $10^9$.
- Message strings contain printable ASCII characters and have lengths up to $100$.
Examples
Example 1
Input: event_tracker(['record', 'record', 'record', 'get_ordered_messages'], [[10, 'Login'], [5, 'Startup'], [10, 'Welcome'], []])
Output: [None, None, None, ['Startup', 'Login', 'Welcome']]
Example 2
Input: event_tracker(['get_ordered_messages', 'record', 'get_ordered_messages'], [[], [1, 'First'], []])
Output: [[], None, ['First']]
Hints
Hint 1
Store a running sequence ID or index alongside each recorded event to preserve insertion order.
Hint 2
Tuple comparison in Python naturally sorts by the first element, then the second element.
Hint 3
When timestamp ties occur, sorting on `(timestamp, insertion_index, message)` ensures stable chronological output without relying on string comparison.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To satisfy the sorting constraints, each incoming event needs to be tagged with both its explicit timestamp and a monotonically increasing sequence counter (representing the arrival order).
When a "record" operation is received, we append (timestamp, sequence, message) to an internal list and increment sequence.
When a "get_ordered_messages" query is triggered, we sort the recorded tuples. Python compares tuples element by element:
timestamp(primary key, ascending order).sequence(secondary key, ensuring events with equal timestamps retain insertion order).
By placing sequence before message in the tuple, we prevent message string content from affecting the tie-breaking logic.
Complexity
Let $K$ be the number of events logged up to a query point.
- Record: $O(1)$ amortized time per operation.
- Get Ordered Messages: $O(K \log K)$ time to sort $K$ entries, and $O(K)$ space to construct the result list.
- Overall Space: $O(K)$ space to store all logged events.
Python 3 Source Code
from typing import List, Optional
def event_tracker(operations: List[str], arguments: List[List]) -> List[Optional[List[str]]]:
events = []
results = []
sequence = 0
for operation, args in zip(operations, arguments):
if operation == "record":
timestamp, message = args
events.append((timestamp, sequence, message))
sequence += 1
results.append(None)
elif operation == "get_ordered_messages":
results.append([event[2] for event in sorted(events)])
else:
raise ValueError("unknown operation")
return results