KatInterview

Trade Join With Expiring Partial Records

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designhash-mapheapsimulation

Last updated

Simulate a stream join that pairs order and price events by ID, using a min-heap with lazy deletion to expire unmatched partial records.

Joining Order and Price Events

A trading pipeline receives two kinds of events that refer to the same trade through a shared order_id:

  • an order event carrying symbol and quantity,
  • a price event carrying price.

A trade is complete only when both halves have arrived, in either order. Until then, the half that has arrived is kept in a pending record. Pending records that wait too long are dropped.

You are given a list of commands and a parallel list of argument lists. Replay them in order and return one result per command.

Commands

CommandArgumentsMeaning
"init"[timeout_ms]Create a fresh engine with the given timeout. It is always the first command.
"process_order"[order_id, symbol, quantity, timestamp_ms]Deliver an order event.
"process_price"[order_id, price, timestamp_ms]Deliver a price event.

Rules for handling an event with timestamp t

  1. Expire first. Before anything else, remove every pending record whose deadline is strictly less than t. A record's deadline is creation_timestamp + timeout_ms, where creation_timestamp is the timestamp of the event that created the record. The only clock is the t of the event being handled. Do not keep a maximum of earlier timestamps, because timestamps may go backwards.
  2. Store the part. If there is no pending record for order_id, create one with deadline t + timeout_ms. Then write the event's data into the record's order part or price part. If that part is already present, overwrite it. Overwriting never changes the record's deadline.
  3. Complete the trade. If the record now has both parts, remove it and produce a completed trade. Otherwise the event produces no output.

An incoming event is never rejected for being late. It always goes through steps 2 and 3 after step 1.

Output

Return a list with one entry per command:

  • None for "init".
  • None for an event that does not complete a trade.
  • For an event that completes a trade, a dictionary with the keys "order_id", "symbol", "quantity", "price". Values are exactly as supplied, with no type conversion.

Constraints

  • The first command is always "init", and it appears once.
  • timeout_ms >= 0; timestamps are integers and may arrive out of order.
  • order_id and symbol are strings; quantity is an integer; price is a number.
  • The number of commands is up to about 10^5, so the work per event should be roughly logarithmic, not proportional to the number of pending records.
  • Commands other than the three above do not occur.

Examples

Example 1

Input: run_trade_join(['init', 'process_order', 'process_price'], [[20], ['101', 'AAPL', 10, 1000], ['101', 150, 1050]])

Output: [None, None, None]

Example 2

Input: run_trade_join(['init', 'process_order', 'process_order', 'process_price'], [[20], ['101', 'AAPL', 10, 1000], ['101', 'AAPL', 10, 1040], ['101', 150, 1041]])

Output: [None, None, None, {'order_id': '101', 'symbol': 'AAPL', 'quantity': 10, 'price': 150}]

Hints

Hint 1

Keep a dictionary from order_id to a pending record holding the order part, the price part, and the record's deadline. A record completes as soon as both parts are present.

Hint 2

Expiry is based on the creation timestamp of the record, not on the latest overwrite. Push (deadline, token, order_id) into a min-heap when a record is created, and pop while the top deadline is strictly less than the current event's timestamp.

Hint 3

A record can be completed and removed, and the same order_id can later start a new record, so heap entries can go stale. Give each record a unique token and delete from the dictionary only if the stored token matches the popped one.