This question evaluates a candidate's ability to enforce deterministic ordering and data normalization of time-series trade execution records, testing competencies in multi-key sorting, tie-breaking logic, and performance with large inputs.
You are building a small component in a trading system that consumes trade execution records from an upstream feed. The records may arrive out of order.
Each trade record has:
trade_id
(string, unique)
symbol
(string)
ts
(integer timestamp in milliseconds)
price
(integer, in ticks)
qty
(integer, > 0)
side
(
"B"
or
"S"
)
Task: Implement a function that takes an array of trade records and returns them sorted in a deterministic “canonical” order:
ts
ts
ties, increasing
symbol
(lexicographic)
trade_id
(lexicographic)
Input/Output:
Constraints (assume):
trade_id
values are unique
Follow-up (optional): Describe how you would handle the case where trade_id is not guaranteed unique (duplicates/replays).