KatInterview

Log Event Processor with Time Order

Data Structure DesignAuto-graded · 14 test cases
EasyAuto-gradedData Structure Designsortingdata-structuresdesign

Last 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:

  1. "record": Takes [timestamp, message] (where timestamp is an integer and message is a string). Records the event and returns None.
  2. "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.