KatInterview

Lifespan Lookups for Orders and Order Groups in a Replay Log

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designhash-mapdesignsimulation

Last updated

Simulate a class through an operations list, tracking per-order and per-group start and end times so lookups run in constant time, with groups reporting 0 while any member is open.

Order and Group Lifespans

A trading system records the lifecycle of orders. Each order may belong to a named group (a "batch"), or may stand alone. You are given a replay of operations performed on a tracker object, and you must return what the tracker would output for each operation.

Operations

You receive two parallel lists, operations and arguments. The i-th operation is operations[i] and its argument list is arguments[i]. Operations are applied in order. Each operation is one of:

  • "process_event" with arguments [timestamp, action, order_id] or [timestamp, action, order_id, batch_id].
    • action is "CREATE": the order order_id opens at timestamp. If a batch_id is given (and is not null), the order is a member of that batch. A CREATE may omit the batch or pass null for a standalone order.
    • action is "CLOSE": the order order_id finishes at timestamp. No batch is supplied; the order's batch is the one it was created with.
  • "get_lifetime" with arguments [identifier]. The identifier may be an order ID or a batch ID.

Output for each operation

  • For process_event, the output is null (None).
  • For get_lifetime, the output is None if the identifier is not a known order or batch, otherwise a two-element list [start, end]:
    • Order ID: start is the order's creation timestamp. end is its close timestamp, or 0 if it has not been closed yet.
    • Batch ID: start is the smallest creation timestamp among all orders created so far in that batch. end is the largest close timestamp among the batch's orders, but it must be 0 if any order of the batch is still open at query time.

Order IDs and batch IDs never collide: no string is used as both. A batch exists as soon as at least one order has been created with that batch ID. A query reflects only the events processed before it.

Return a list with exactly one entry per operation, in order.

Constraints

  • 1 <= timestamp <= 10^9.
  • Each order is created at most once and closed at most once, and never closed before it is created.
  • The total number of operations is at most 200,000.
  • Each get_lifetime should be answerable in O(1) time; do not scan the members of a batch at query time.
  • Order and batch IDs are strings, and the two sets of IDs are disjoint.

Examples

Example 1

Input: replay_lifespans(['process_event', 'process_event', 'process_event', 'process_event', 'get_lifetime', 'get_lifetime', 'get_lifetime', 'get_lifetime', 'process_event', 'get_lifetime', 'get_lifetime'], [[100, 'CREATE', 'O1', 'B1'], [105, 'CREATE', 'O2', 'B1'], [110, 'CREATE', 'O3', None], [150, 'CLOSE', 'O1'], ['O1'], ['O2'], ['B1'], ['O3'], [180, 'CLOSE', 'O2'], ['B1'], ['UNKNOWN']])

Output: [None, None, None, None, [100, 150], [105, 0], [100, 0], [110, 0], None, [100, 180], None]

Hints

Hint 1

A query must not iterate over batch members, so each batch needs a summary that you keep up to date as events arrive.

Hint 2

For each batch, keep the earliest creation time, the latest close time seen so far, and a count of members that are still open.

Hint 3

A batch's end must be reported as 0 whenever its open count is above zero, even if some members have already closed and a latest close time exists. Do not use 'latest close is 0' as the open test.