Implement price-based order matcher
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates competency in data structures and algorithmic design for price-based order matching systems, including priority-based matching, efficient order book maintenance, and deterministic tie-breaking requirements.
Read the full Optiver Software Engineer interview experience this question came from
Constraints
- 0 <= len(orders) <= 200000
- Each order is of the form [type, price]
- type is either 1 or -1
- 1 <= price <= 10^9
- The answer fits in a signed 64-bit integer
Examples
Input: ([],)
Expected Output: 0
Explanation: There are no orders, so no trades occur.
Input: ([[1, 100]],)
Expected Output: 0
Explanation: A single buy order cannot trade because there is no resting sell order.
Hints
- You need fast access to the cheapest resting sell and the most expensive resting buy.
- To break ties deterministically for equal prices, store an arrival index along with each order.