Sort trade executions into a canonical order
Jane StreetSoftware Engineer · OnsiteLast updated
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:
- Increasing
ts - If
tsties, increasingsymbol(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_idvalues 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).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.