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:
-
Increasing
ts
-
If
ts
ties, increasing
symbol
(lexicographic)
-
If still tied, increasing
trade_id
(lexicographic)
Input/Output:
-
Input: list of trade records
-
Output: the same records sorted by the rule above
Constraints (assume):
-
Up to 2e5 trades
-
trade_id
values are unique
-
Sorting must be deterministic
Follow-up (optional): Describe how you would handle the case where trade_id is not guaranteed unique (duplicates/replays).